Skip to content

Commit 85f5993

Browse files
authored
Add examples to FeatureGuard/FeatureSwitchDefinition docs (#10304)
1 parent a614b2e commit 85f5993

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

xml/System.Diagnostics.CodeAnalysis/FeatureGuardAttribute.xml

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,38 @@
2929
</Attributes>
3030
<Docs>
3131
<summary>
32-
<para>Indicates that the specified public static boolean get-only property</para>
33-
<para>guards access to the specified feature.</para>
32+
<para>
33+
Indicates that the specified public static Boolean read-only property
34+
guards access to the specified feature.</para>
3435
</summary>
3536
<remarks>
36-
<para>Analyzers can use this to prevent warnings on calls to code that is</para>
37-
<para>annotated as requiring that feature, when the callsite is guarded by a</para>
38-
<para>call to the property.</para>
37+
<para>
38+
Analyzers can use this attribute to prevent warnings on calls to code that's
39+
annotated as requiring that feature, when the callsite is guarded by a
40+
call to the property.</para>
3941
</remarks>
42+
<example>
43+
<format type="text/markdown"><![CDATA[
44+
```csharp
45+
if (Feature.IsSupported)
46+
Feature.Implementation();
47+
48+
public class Feature
49+
{
50+
[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
51+
internal static bool IsSupported => RuntimeFeature.IsDynamicCodeSupported;
52+
53+
[RequiresDynamicCode("Feature requires dynamic code support.")]
54+
internal static Implementation() => ...; // Uses dynamic code
55+
}
56+
```
57+
58+
When the app is built with `<PublishAot>true</PublishAot>`, the call to
59+
`Feature.Implementation()` doesn't produce analyzer warning
60+
[IL3050](/dotnet/core/deploying/native-aot/warnings/il3050)
61+
and `Feature.Implementation` code is removed when publishing.
62+
]]></format>
63+
</example>
4064
</Docs>
4165
<Members>
4266
<Member MemberName=".ctor">

xml/System.Diagnostics.CodeAnalysis/FeatureSwitchDefinitionAttribute.xml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,40 @@
2929
</Attributes>
3030
<Docs>
3131
<summary>
32-
<para>Indicates that the specified public static boolean get-only property</para>
33-
<para>corresponds to the feature switch specified by name.</para>
32+
<para>
33+
Indicates that the specified public static Boolean read-only property
34+
corresponds to the feature switch specified by name.</para>
3435
</summary>
3536
<remarks>
36-
<para>IL rewriters and compilers can use this to substitute the return value</para>
37-
<para>of the specified property with the value of the feature switch.</para>
37+
<para>
38+
IL rewriters and compilers can use this attribute to substitute the return value
39+
of the specified property with the value of the feature switch.</para>
3840
</remarks>
41+
<example>
42+
<format type="text/markdown"><![CDATA[
43+
```csharp
44+
if (Feature.IsSupported)
45+
Feature.Implementation();
46+
47+
public class Feature
48+
{
49+
[FeatureSwitchDefinition("Feature.IsSupported")]
50+
internal static bool IsSupported => AppContext.TryGetSwitch("Feature.IsSupported", out bool isEnabled) ? isEnabled : true;
51+
52+
internal static Implementation() => ...;
53+
}
54+
```
55+
56+
When the app is trimmed with the following feature settings in the project file,
57+
`Feature.IsSupported` is treated as `false`, and `Feature.Implementation` code is removed.
58+
59+
```xml
60+
<ItemGroup>
61+
<RuntimeHostConfigurationOption Include="Feature.IsSupported" Value="false" />
62+
</ItemGroup>
63+
```
64+
]]></format>
65+
</example>
3966
</Docs>
4067
<Members>
4168
<Member MemberName=".ctor">

0 commit comments

Comments
 (0)