Skip to content

Commit a8891ba

Browse files
authored
System.AttributeUsageAttribute F# snippets (#7526)
* System.AttributeUsageAttribute F# snippets * Update AttributeUsageAttribute.xml
1 parent 4e1212f commit a8891ba

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="source.fs" />
8+
</ItemGroup>
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// <Snippet1>
2+
namespace System.Runtime.InteropServices
3+
4+
open System
5+
6+
[<AttributeUsage(AttributeTargets.Method ||| AttributeTargets.Field ||| AttributeTargets.Property)>]
7+
type DispIdAttribute(value: int) =
8+
inherit Attribute()
9+
10+
// . . .
11+
12+
member _.Value with get() =
13+
// . . .
14+
0
15+
16+
// </Snippet1>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
open System
2+
// <Snippet1>
3+
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Field, Inherited = true)>]
4+
type InheritedAttribute() =
5+
inherit Attribute()
6+
7+
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Field, Inherited = false)>]
8+
type NotInheritedAttribute() =
9+
inherit Attribute()
10+
// </Snippet1>
11+
12+
// <Snippet2>
13+
[<Inherited>]
14+
type BaseA() =
15+
abstract member MethodA: unit -> unit
16+
[<Inherited>]
17+
default _.MethodA() = ()
18+
19+
type DerivedA() =
20+
inherit BaseA()
21+
22+
override _.MethodA() = ()
23+
24+
[<NotInherited>]
25+
type BaseB() =
26+
abstract member MethodB: unit -> unit
27+
[<NotInherited>]
28+
default _.MethodB() = ()
29+
30+
type DerivedB() =
31+
inherit BaseB()
32+
33+
override _.MethodB() = ()
34+
35+
let typeA = typeof<DerivedA>
36+
printfn $"DerivedA has Inherited attribute: {typeA.GetCustomAttributes(typeof<InheritedAttribute>, true).Length > 0}"
37+
let memberA = typeA.GetMethod "MethodA"
38+
printfn $"DerivedA.MemberA has Inherited attribute: {memberA.GetCustomAttributes(typeof<InheritedAttribute>, true).Length > 0}\n"
39+
40+
let typeB = typeof<DerivedB>
41+
printfn $"DerivedB has NotInherited attribute: {typeB.GetCustomAttributes(typeof<NotInheritedAttribute>, true).Length > 0}"
42+
let memberB = typeB.GetMethod "MethodB"
43+
printfn $"DerivedB.MemberB has NotInherited attribute: {memberB.GetCustomAttributes(typeof<NotInheritedAttribute>, true).Length > 0}"
44+
45+
46+
// The example displays the following output:
47+
// DerivedA has Inherited attribute: True
48+
// DerivedA.MemberA has Inherited attribute: True
49+
//
50+
// DerivedB has NotInherited attribute: False
51+
// DerivedB.MemberB has NotInherited attribute: False
52+
// </Snippet2>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Inherited1.fs" />
8+
</ItemGroup>
9+
</Project>

xml/System/AttributeUsageAttribute.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
140140
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_Classic/classic AttributeUsageAttribute.AttributeUsageAttribute Example/CPP/source.cpp" id="Snippet1":::
141141
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_Classic/classic AttributeUsageAttribute.AttributeUsageAttribute Example/CS/source.cs" id="Snippet1":::
142+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_Classic/classic AttributeUsageAttribute.AttributeUsageAttribute Example/FS/source.fs" id="Snippet1":::
142143
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_Classic/classic AttributeUsageAttribute.AttributeUsageAttribute Example/VB/source.vb" id="Snippet1":::
143144
144145
]]></format>
@@ -251,11 +252,13 @@
251252
The following example illustrates the difference between an attribute to which an <xref:System.AttributeUsageAttribute> attribute with an <xref:System.AttributeUsageAttribute.Inherited%2A> property value of `true` is applied and one to which <xref:System.AttributeUsageAttribute> attribute with an <xref:System.AttributeUsageAttribute.Inherited%2A> property value of `false` is applied. The example defines two attributes, `InheritedAttribute` and `NotInheritedAttribute`. Both attributes can apply to classes and methods. Because the <xref:System.AttributeUsageAttribute.Inherited%2A> property of the <xref:System.AttributeUsageAttribute> attribute applied to `InheritedAttribute` is `true`, it is inherited by derived classes and the members of derived classes that override the base class method. On the other hand, because the <xref:System.AttributeUsageAttribute.Inherited%2A> property of the <xref:System.AttributeUsageAttribute> attribute applied to `NotInheritedAttribute` is `false`, it is not inherited by derived classes and the members of derived classes that override the base class method.
252253
253254
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs" id="Snippet1":::
255+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/fs/Inherited1.fs" id="Snippet1":::
254256
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/vb/Inherited1.vb" id="Snippet1":::
255257
256258
The example then defines two base classes. The first, `BaseA`, has a single method, `MethodA`. The second, `BaseB`, has a single method, `MethodB`. `BaseA` and `MethodA` are tagged with the `InheritedAttribute` attribute, and `BaseB` and `MethodB` are tagged with the `NotInheritedAttribute` attribute. `DerivedA` inherits from `BaseA` and overrides its `MethodA` method. `DerivedB` inherits from `BaseB` and overrides its `MethodB` method.
257259
258260
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs" id="Snippet2":::
261+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/fs/Inherited1.fs" id="Snippet2":::
259262
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/vb/Inherited1.vb" id="Snippet2":::
260263
261264
As the output from the example shows, `DerivedA` and `DerivedA.MethodA` inherit the `InheritedAttribute` attribute, but `DerivedB` and `DerivedB.MethodB` do not inherit the `NotInheritedAttribute` attribute.

0 commit comments

Comments
 (0)