You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<summary>Provides access to an inaccessible member of a specific type.</summary>
32
+
<remarks>
33
33
<formattype="text/markdown"><![CDATA[
34
34
35
35
## Remarks
36
36
37
37
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>.
38
38
39
-
40
-
41
39
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.
42
40
43
41
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
46
44
47
45
The value of the first argument is not used by the implementation for `static` fields and methods.
48
46
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
+
50
49
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.
51
54
52
55
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>
57
60
<codelanguage="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)
59
102
{
60
103
PrivateMethod(c);
104
+
61
105
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(PrivateMethod))]
<paramname="kind">The kind of the target to which access is provided.</param>
85
-
<summary>Instantiates an <seecref="T:System.Runtime.CompilerServices.UnsafeAccessorAttribute" /> providing access to a member of kind <seecref="T:System.Runtime.CompilerServices.UnsafeAccessorKind" />.</summary>
<paramname="kind">The kind of the target to which access is provided.</param>
137
+
<summary>Instantiates an <seecref="T:System.Runtime.CompilerServices.UnsafeAccessorAttribute" /> providing access to a member of kind <seecref="T:System.Runtime.CompilerServices.UnsafeAccessorKind" />.</summary>
0 commit comments