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
The [UnsafeAccessorAttribute] (API Proposal) is used on static extern functions to instruct the .NET runtime to generate an accessor method to a type member that bypass the normal .NET visibility checks.
publicclassMyClass{privateint_hidden;
...}publicstaticpartialclassAccessHelpers{[UnsafeAccessor(UnsafeAccessorKind.Field, Name="_hidden")]publicstaticexternrefintGetHiddenField(MyClassc);// runtime generated implementation}
...publicstatic MyClass DeserializeMyClass(string s){
MyClass x =new MyClass();ref int hiddenFieldRef =ref AccessHelpers.GetHiddenField(x);hiddenFieldRef=int.Parse(s);returnx;}}
With [UnsafeAccessor] it becomes possible to use source-generated reflection-free serializers that can serialize and deserialize fields of user-defined classes. For example, a user may add a new field _hidden to MyClass and the source generator will add a new accessor method to AccessHelpers and generate a method DeserializeMyClass that uses the access method to populate the hidden field.
Currently if the user adds a second private field _hidden2 and the generator adds a new static extern method, EnC will report a rude edit:
error ENC0025: Adding an extern method requires restarting the application.
Note that both AccessHelpers or the static extern method could be generic:
publicclassMyContainer<T>{private T[]_elements;}publicstaticAccessHelpers<T>{[UnsafeAccessor(UnsafeAccessorKind.Field, Name="_elements")]
public static extern ref T[] GetElements(MyContainer<T>container);}
public static AccessHelpers2
{[UnsafeAccessor(UnsafeAccessorKind.Field, Name="_elements")]
public static extern ref T[] GetElements<T>(MyContainer<T>container);}
Note these are not P/Invokes. extern functions dont' have any special flags set. Instead they just don't have a method body RVA (and thus no method header or IL)
The
[UnsafeAccessorAttribute]
(API Proposal) is used onstatic extern
functions to instruct the .NET runtime to generate an accessor method to a type member that bypass the normal .NET visibility checks.With
[UnsafeAccessor]
it becomes possible to use source-generated reflection-free serializers that can serialize and deserialize fields of user-defined classes. For example, a user may add a new field_hidden
toMyClass
and the source generator will add a new accessor method toAccessHelpers
and generate a methodDeserializeMyClass
that uses the access method to populate the hidden field.Currently if the user adds a second private field
_hidden2
and the generator adds a newstatic extern
method, EnC will report a rude edit:Note that both
AccessHelpers
or thestatic extern
method could be generic:The runtime support is tracked in dotnet/runtime#102080
The text was updated successfully, but these errors were encountered: