Skip to content

Commit 63e3266

Browse files
committed
Add examples to Feature attributes
1 parent 3b11856 commit 63e3266

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

xml/System.Diagnostics.CodeAnalysis/FeatureGuardAttribute.xml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,35 @@
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 get-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 to prevent warnings on calls to code that is
39+
annotated as requiring that feature, when the callsite is guarded by a
40+
call to the property.</para>
3941
</remarks>
42+
<example>
43+
<code language="csharp">
44+
if (Feature.IsSupported)
45+
Feature.Implementation();
46+
47+
public class Feature
48+
{
49+
[FeatureGuard(typeof(RequiresDynamicCodeAttribute))]
50+
internal static bool IsSupported => RuntimeFeature.IsDynamicCodeSupported;
51+
52+
[RequiresDynamicCode("Feature requires dynamic code support.")]
53+
internal static Implementation() => ...; // Uses dynamic code
54+
}
55+
</code>
56+
When the app is built with `&ltPublishAot&gt;true&lt/PublishAot&gt;`, the call to
57+
`Feature.Implementation()` doesn't produce analyzer warning
58+
<see href="https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/warnings/il3050">IL3050</see>,
59+
and `Feature.Implementation` code is removed when publishing.
60+
</example>
4061
</Docs>
4162
<Members>
4263
<Member MemberName=".ctor">

xml/System.Diagnostics.CodeAnalysis/FeatureSwitchDefinitionAttribute.xml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,36 @@
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 get-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 to substitute the return value
39+
of the specified property with the value of the feature switch.</para>
3840
</remarks>
41+
<example>
42+
<code language="csharp">
43+
if (Feature.IsSupported)
44+
Feature.Implementation();
45+
46+
public class Feature
47+
{
48+
[FeatureSwitchDefinition("Feature.IsSupported")]
49+
internal static bool IsSupported => AppContext.TryGetSwitch("Feature.IsSupported", out bool isEnabled) ? isEnabled : true;
50+
51+
internal static Implementation() => ...;
52+
}
53+
</code>
54+
When the app is trimmed with the following feature settings in the project file,
55+
`Feature.IsSupported` is treated as `false`, and `Feature.Implementation` code is removed.
56+
<code language="xml">
57+
&lt;ItemGroup&gt;
58+
&lt;RuntimeHostConfigurationOption Include="Feature.IsSupported" Value="false" /&gt;
59+
&lt;/ItemGroup&gt;
60+
</code>
61+
</example>
3962
</Docs>
4063
<Members>
4164
<Member MemberName=".ctor">

0 commit comments

Comments
 (0)