Skip to content

Commit e97784b

Browse files
Update UnsafeAccessorAttribute.xml (#9499)
* Update UnsafeAccessorAttribute.xml with more examples
1 parent b2716ce commit e97784b

File tree

1 file changed

+161
-109
lines changed

1 file changed

+161
-109
lines changed
Lines changed: 161 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
1-
<Type Name="UnsafeAccessorAttribute" FullName="System.Runtime.CompilerServices.UnsafeAccessorAttribute">
2-
<TypeSignature Language="C#" Value="public sealed class UnsafeAccessorAttribute : Attribute" />
3-
<TypeSignature Language="ILAsm" Value=".class public auto ansi sealed beforefieldinit UnsafeAccessorAttribute extends System.Attribute" />
4-
<TypeSignature Language="DocId" Value="T:System.Runtime.CompilerServices.UnsafeAccessorAttribute" />
5-
<TypeSignature Language="VB.NET" Value="Public NotInheritable Class UnsafeAccessorAttribute&#xA;Inherits Attribute" />
6-
<TypeSignature Language="F#" Value="type UnsafeAccessorAttribute = class&#xA; inherit Attribute" />
7-
<TypeSignature Language="C++ CLI" Value="public ref class UnsafeAccessorAttribute sealed : Attribute" />
8-
<AssemblyInfo>
9-
<AssemblyName>System.Runtime</AssemblyName>
10-
<AssemblyVersion>8.0.0.0</AssemblyVersion>
11-
</AssemblyInfo>
12-
<Base>
13-
<BaseTypeName>System.Attribute</BaseTypeName>
14-
</Base>
15-
<Interfaces />
16-
<Attributes>
17-
<Attribute>
18-
<AttributeName Language="C#">[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]</AttributeName>
19-
<AttributeName Language="F#">[&lt;System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)&gt;]</AttributeName>
20-
</Attribute>
21-
<Attribute>
22-
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(0)]</AttributeName>
23-
<AttributeName Language="F#">[&lt;System.Runtime.CompilerServices.Nullable(0)&gt;]</AttributeName>
24-
</Attribute>
25-
<Attribute>
26-
<AttributeName Language="C#">[System.Runtime.CompilerServices.NullableContext(2)]</AttributeName>
27-
<AttributeName Language="F#">[&lt;System.Runtime.CompilerServices.NullableContext(2)&gt;]</AttributeName>
28-
</Attribute>
29-
</Attributes>
30-
<Docs>
31-
<summary>Provides access to an inaccessible member of a specific type.</summary>
32-
<remarks>
1+
<Type Name="UnsafeAccessorAttribute" FullName="System.Runtime.CompilerServices.UnsafeAccessorAttribute">
2+
<TypeSignature Language="C#" Value="public sealed class UnsafeAccessorAttribute : Attribute" />
3+
<TypeSignature Language="ILAsm" Value=".class public auto ansi sealed beforefieldinit UnsafeAccessorAttribute extends System.Attribute" />
4+
<TypeSignature Language="DocId" Value="T:System.Runtime.CompilerServices.UnsafeAccessorAttribute" />
5+
<TypeSignature Language="VB.NET" Value="Public NotInheritable Class UnsafeAccessorAttribute&#xA;Inherits Attribute" />
6+
<TypeSignature Language="F#" Value="type UnsafeAccessorAttribute = class&#xA; inherit Attribute" />
7+
<TypeSignature Language="C++ CLI" Value="public ref class UnsafeAccessorAttribute sealed : Attribute" />
8+
<AssemblyInfo>
9+
<AssemblyName>System.Runtime</AssemblyName>
10+
<AssemblyVersion>8.0.0.0</AssemblyVersion>
11+
</AssemblyInfo>
12+
<Base>
13+
<BaseTypeName>System.Attribute</BaseTypeName>
14+
</Base>
15+
<Interfaces />
16+
<Attributes>
17+
<Attribute>
18+
<AttributeName Language="C#">[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]</AttributeName>
19+
<AttributeName Language="F#">[&lt;System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)&gt;]</AttributeName>
20+
</Attribute>
21+
<Attribute>
22+
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(0)]</AttributeName>
23+
<AttributeName Language="F#">[&lt;System.Runtime.CompilerServices.Nullable(0)&gt;]</AttributeName>
24+
</Attribute>
25+
<Attribute>
26+
<AttributeName Language="C#">[System.Runtime.CompilerServices.NullableContext(2)]</AttributeName>
27+
<AttributeName Language="F#">[&lt;System.Runtime.CompilerServices.NullableContext(2)&gt;]</AttributeName>
28+
</Attribute>
29+
</Attributes>
30+
<Docs>
31+
<summary>Provides access to an inaccessible member of a specific type.</summary>
32+
<remarks>
3333
<format type="text/markdown"><![CDATA[
3434
3535
## Remarks
3636
3737
You can apply this attribute to an `extern static` method. The implementation of the `extern static` method annotated with this attribute will be provided by the runtime based on the information in the attribute and the signature of the method that the attribute is applied to. The runtime will try to find the matching method or field and forward the call to it. If the matching method or field is not found, the body of the `extern static` method will throw <xref:System.MissingFieldException> or <xref:System.MissingMethodException>.
3838
39-
40-
4139
For <xref:System.Runtime.CompilerServices.UnsafeAccessorKind.Method>, <xref:System.Runtime.CompilerServices.UnsafeAccessorKind.StaticMethod>, <xref:System.Runtime.CompilerServices.UnsafeAccessorKind.Field>, and <xref:System.Runtime.CompilerServices.UnsafeAccessorKind.StaticField>, the type of the first argument of the annotated `extern static` method identifies the owning type. Only the specific type defined will be examined for inaccessible members. The type hierarchy is not walked looking for a match.
4240
4341
The value of the first argument is treated as `this` pointer for instance fields and methods.
@@ -46,88 +44,142 @@ The first argument must be passed as `ref` for instance fields and methods on st
4644
4745
The value of the first argument is not used by the implementation for `static` fields and methods.
4846
49-
The return type is considered for the signature match. modreqs and modopts are initially not considered for the signature match. However, if an ambiguity exists ignoring modreqs and modopts, a precise match is attempted. If an ambiguity still exists, <xref:System.Reflection.AmbiguousMatchException> is thrown.
47+
The return value for an accessor to a field can be `ref` if setting of the field is desired.
48+
5049
50+
Constructors can be accessed using <xref:System.Runtime.CompilerServices.UnsafeAccessorKind.Constructor> or <xref:System.Runtime.CompilerServices.UnsafeAccessorKind.Method>.
51+
52+
53+
The return type is considered for the signature match. Modreqs and modopts are initially not considered for the signature match. However, if an ambiguity exists ignoring modreqs and modopts, a precise match is attempted. If an ambiguity still exists, <xref:System.Reflection.AmbiguousMatchException> is thrown.
5154
5255
By default, the attributed method's name dictates the name of the method/field. This can cause confusion in some cases since language abstractions, like C# local functions, generate mangled IL names. The solution to this is to use the `nameof` mechanism and define the <xref:System.Runtime.CompilerServices.UnsafeAccessorAttribute.Name> property.
53-
54-
]]></format>
55-
</remarks>
56-
<example>
56+
57+
]]></format>
58+
</remarks>
59+
<example>
5760
<code language="csharp">
58-
public void Method(Class c)
61+
public class Class
62+
{
63+
static void StaticPrivateMethod() { }
64+
static int StaticPrivateField;
65+
Class(int i) { PrivateField = i; }
66+
void PrivateMethod() { }
67+
int PrivateField;
68+
}
69+
70+
public void CallStaticPrivateMethod()
71+
{
72+
StaticPrivateMethod(null);
73+
74+
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = nameof(StaticPrivateMethod))]
75+
extern static void StaticPrivateMethod(Class c);
76+
}
77+
public void GetSetStaticPrivateField()
78+
{
79+
ref int f = ref GetSetStaticPrivateField(null);
80+
81+
82+
[UnsafeAccessor(UnsafeAccessorKind.StaticField, Name = "StaticPrivateField")]
83+
extern static ref int GetSetStaticPrivateField(Class c);
84+
}
85+
public void CallPrivateConstructor()
86+
{
87+
Class c1 = PrivateCtor(1);
88+
89+
Class c2 = (Class)RuntimeHelpers.GetUninitializedObject(typeof(Class));
90+
91+
PrivateCtorAsMethod(c2, 2);
92+
93+
94+
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
95+
extern static Class PrivateCtor(int i);
96+
97+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = ".ctor")]
98+
extern static void PrivateCtorAsMethod(Class c, int i);
99+
100+
}
101+
public void CallPrivateMethod(Class c)
59102
{
60103
PrivateMethod(c);
104+
61105
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(PrivateMethod))]
62106
extern static void PrivateMethod(Class c);
63107
}
64-
</code>
65-
</example>
66-
</Docs>
67-
<Members>
68-
<Member MemberName=".ctor">
69-
<MemberSignature Language="C#" Value="public UnsafeAccessorAttribute (System.Runtime.CompilerServices.UnsafeAccessorKind kind);" />
70-
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(valuetype System.Runtime.CompilerServices.UnsafeAccessorKind kind) cil managed" />
71-
<MemberSignature Language="DocId" Value="M:System.Runtime.CompilerServices.UnsafeAccessorAttribute.#ctor(System.Runtime.CompilerServices.UnsafeAccessorKind)" />
72-
<MemberSignature Language="VB.NET" Value="Public Sub New (kind As UnsafeAccessorKind)" />
73-
<MemberSignature Language="F#" Value="new System.Runtime.CompilerServices.UnsafeAccessorAttribute : System.Runtime.CompilerServices.UnsafeAccessorKind -&gt; System.Runtime.CompilerServices.UnsafeAccessorAttribute" Usage="new System.Runtime.CompilerServices.UnsafeAccessorAttribute kind" />
74-
<MemberSignature Language="C++ CLI" Value="public:&#xA; UnsafeAccessorAttribute(System::Runtime::CompilerServices::UnsafeAccessorKind kind);" />
75-
<MemberType>Constructor</MemberType>
76-
<AssemblyInfo>
77-
<AssemblyName>System.Runtime</AssemblyName>
78-
<AssemblyVersion>8.0.0.0</AssemblyVersion>
79-
</AssemblyInfo>
80-
<Parameters>
81-
<Parameter Name="kind" Type="System.Runtime.CompilerServices.UnsafeAccessorKind" />
82-
</Parameters>
83-
<Docs>
84-
<param name="kind">The kind of the target to which access is provided.</param>
85-
<summary>Instantiates an <see cref="T:System.Runtime.CompilerServices.UnsafeAccessorAttribute" /> providing access to a member of kind <see cref="T:System.Runtime.CompilerServices.UnsafeAccessorKind" />.</summary>
86-
<remarks>To be added.</remarks>
87-
</Docs>
88-
</Member>
89-
<Member MemberName="Kind">
90-
<MemberSignature Language="C#" Value="public System.Runtime.CompilerServices.UnsafeAccessorKind Kind { get; }" />
91-
<MemberSignature Language="ILAsm" Value=".property instance valuetype System.Runtime.CompilerServices.UnsafeAccessorKind Kind" />
92-
<MemberSignature Language="DocId" Value="P:System.Runtime.CompilerServices.UnsafeAccessorAttribute.Kind" />
93-
<MemberSignature Language="VB.NET" Value="Public ReadOnly Property Kind As UnsafeAccessorKind" />
94-
<MemberSignature Language="F#" Value="member this.Kind : System.Runtime.CompilerServices.UnsafeAccessorKind" Usage="System.Runtime.CompilerServices.UnsafeAccessorAttribute.Kind" />
95-
<MemberSignature Language="C++ CLI" Value="public:&#xA; property System::Runtime::CompilerServices::UnsafeAccessorKind Kind { System::Runtime::CompilerServices::UnsafeAccessorKind get(); };" />
96-
<MemberType>Property</MemberType>
97-
<AssemblyInfo>
98-
<AssemblyName>System.Runtime</AssemblyName>
99-
<AssemblyVersion>8.0.0.0</AssemblyVersion>
100-
</AssemblyInfo>
101-
<ReturnValue>
102-
<ReturnType>System.Runtime.CompilerServices.UnsafeAccessorKind</ReturnType>
103-
</ReturnValue>
104-
<Docs>
105-
<summary>Gets the kind of member to which access is provided.</summary>
106-
<value>To be added.</value>
107-
<remarks>To be added.</remarks>
108-
</Docs>
109-
</Member>
110-
<Member MemberName="Name">
111-
<MemberSignature Language="C#" Value="public string? Name { get; set; }" />
112-
<MemberSignature Language="ILAsm" Value=".property instance string Name" />
113-
<MemberSignature Language="DocId" Value="P:System.Runtime.CompilerServices.UnsafeAccessorAttribute.Name" />
114-
<MemberSignature Language="VB.NET" Value="Public Property Name As String" />
115-
<MemberSignature Language="F#" Value="member this.Name : string with get, set" Usage="System.Runtime.CompilerServices.UnsafeAccessorAttribute.Name" />
116-
<MemberSignature Language="C++ CLI" Value="public:&#xA; property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };" />
117-
<MemberType>Property</MemberType>
118-
<AssemblyInfo>
119-
<AssemblyName>System.Runtime</AssemblyName>
120-
<AssemblyVersion>8.0.0.0</AssemblyVersion>
121-
</AssemblyInfo>
122-
<ReturnValue>
123-
<ReturnType>System.String</ReturnType>
124-
</ReturnValue>
125-
<Docs>
126-
<summary>Gets or sets the name of the member to which access is provided.</summary>
127-
<value>To be added.</value>
108+
public void GetSetPrivateField(Class c)
109+
{
110+
ref int f = ref GetSetPrivateField(c);
111+
112+
113+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "PrivateField")]
114+
extern static ref int GetSetPrivateField(Class c);
115+
}
116+
</code>
117+
</example>
118+
</Docs>
119+
<Members>
120+
<Member MemberName=".ctor">
121+
<MemberSignature Language="C#" Value="public UnsafeAccessorAttribute (System.Runtime.CompilerServices.UnsafeAccessorKind kind);" />
122+
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(valuetype System.Runtime.CompilerServices.UnsafeAccessorKind kind) cil managed" />
123+
<MemberSignature Language="DocId" Value="M:System.Runtime.CompilerServices.UnsafeAccessorAttribute.#ctor(System.Runtime.CompilerServices.UnsafeAccessorKind)" />
124+
<MemberSignature Language="VB.NET" Value="Public Sub New (kind As UnsafeAccessorKind)" />
125+
<MemberSignature Language="F#" Value="new System.Runtime.CompilerServices.UnsafeAccessorAttribute : System.Runtime.CompilerServices.UnsafeAccessorKind -&gt; System.Runtime.CompilerServices.UnsafeAccessorAttribute" Usage="new System.Runtime.CompilerServices.UnsafeAccessorAttribute kind" />
126+
<MemberSignature Language="C++ CLI" Value="public:&#xA; UnsafeAccessorAttribute(System::Runtime::CompilerServices::UnsafeAccessorKind kind);" />
127+
<MemberType>Constructor</MemberType>
128+
<AssemblyInfo>
129+
<AssemblyName>System.Runtime</AssemblyName>
130+
<AssemblyVersion>8.0.0.0</AssemblyVersion>
131+
</AssemblyInfo>
132+
<Parameters>
133+
<Parameter Name="kind" Type="System.Runtime.CompilerServices.UnsafeAccessorKind" />
134+
</Parameters>
135+
<Docs>
136+
<param name="kind">The kind of the target to which access is provided.</param>
137+
<summary>Instantiates an <see cref="T:System.Runtime.CompilerServices.UnsafeAccessorAttribute" /> providing access to a member of kind <see cref="T:System.Runtime.CompilerServices.UnsafeAccessorKind" />.</summary>
138+
<remarks>To be added.</remarks>
139+
</Docs>
140+
</Member>
141+
<Member MemberName="Kind">
142+
<MemberSignature Language="C#" Value="public System.Runtime.CompilerServices.UnsafeAccessorKind Kind { get; }" />
143+
<MemberSignature Language="ILAsm" Value=".property instance valuetype System.Runtime.CompilerServices.UnsafeAccessorKind Kind" />
144+
<MemberSignature Language="DocId" Value="P:System.Runtime.CompilerServices.UnsafeAccessorAttribute.Kind" />
145+
<MemberSignature Language="VB.NET" Value="Public ReadOnly Property Kind As UnsafeAccessorKind" />
146+
<MemberSignature Language="F#" Value="member this.Kind : System.Runtime.CompilerServices.UnsafeAccessorKind" Usage="System.Runtime.CompilerServices.UnsafeAccessorAttribute.Kind" />
147+
<MemberSignature Language="C++ CLI" Value="public:&#xA; property System::Runtime::CompilerServices::UnsafeAccessorKind Kind { System::Runtime::CompilerServices::UnsafeAccessorKind get(); };" />
148+
<MemberType>Property</MemberType>
149+
<AssemblyInfo>
150+
<AssemblyName>System.Runtime</AssemblyName>
151+
<AssemblyVersion>8.0.0.0</AssemblyVersion>
152+
</AssemblyInfo>
153+
<ReturnValue>
154+
<ReturnType>System.Runtime.CompilerServices.UnsafeAccessorKind</ReturnType>
155+
</ReturnValue>
156+
<Docs>
157+
<summary>Gets the kind of member to which access is provided.</summary>
158+
<value>To be added.</value>
159+
<remarks>To be added.</remarks>
160+
</Docs>
161+
</Member>
162+
<Member MemberName="Name">
163+
<MemberSignature Language="C#" Value="public string? Name { get; set; }" />
164+
<MemberSignature Language="ILAsm" Value=".property instance string Name" />
165+
<MemberSignature Language="DocId" Value="P:System.Runtime.CompilerServices.UnsafeAccessorAttribute.Name" />
166+
<MemberSignature Language="VB.NET" Value="Public Property Name As String" />
167+
<MemberSignature Language="F#" Value="member this.Name : string with get, set" Usage="System.Runtime.CompilerServices.UnsafeAccessorAttribute.Name" />
168+
<MemberSignature Language="C++ CLI" Value="public:&#xA; property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };" />
169+
<MemberType>Property</MemberType>
170+
<AssemblyInfo>
171+
<AssemblyName>System.Runtime</AssemblyName>
172+
<AssemblyVersion>8.0.0.0</AssemblyVersion>
173+
</AssemblyInfo>
174+
<ReturnValue>
175+
<ReturnType>System.String</ReturnType>
176+
</ReturnValue>
177+
<Docs>
178+
<summary>Gets or sets the name of the member to which access is provided.</summary>
179+
<value>To be added.</value>
128180
<remarks>The name defaults to the annotated method name if not specified.
129-
The name must be unset/<code>null</code> for <see cref="F:System.Runtime.CompilerServices.UnsafeAccessorKind.Constructor" />.</remarks>
130-
</Docs>
131-
</Member>
132-
</Members>
133-
</Type>
181+
The name must be unset/<code>null</code> for <see cref="F:System.Runtime.CompilerServices.UnsafeAccessorKind.Constructor" />.</remarks>
182+
</Docs>
183+
</Member>
184+
</Members>
185+
</Type>

0 commit comments

Comments
 (0)