-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Add support for skipping visibility checks with AssemblyBuilder.DefineDynamicAssembly #85020
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection-emit Issue DetailsBackground and motivationDynamicMethod allows accessing non public elements with its API Proposalnamespace System.Reflection.Emit;
public class AssemblyBuilder
{
public static System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, bool restrictedSkipVisibility);
public static System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder>? assemblyAttributes, bool restrictedSkipVisibility);
} API UsageAssemblyName aName = new AssemblyName("DynamicAssemblyExample");
AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run, true);
// emit module and types
ConstructorInfo ctorOfSomeNonPublicTypeImplementingISomeInterface = ...
MethodBuilder meth = tb.DefineMethod(
"MyMethod",
MethodAttributes.Public,
typeof(ISomeInterface),
new Type[] { });
ILGenerator methIL = meth.GetILGenerator();
methIL.Emit(OpCodes.Newobj, ctorOfSomeNonPublicTypeImplementingISomeInterface);
methIL.Emit(OpCodes.Ret); Alternative DesignsDocument and expose the hidden RisksAccessing members that shouldn't be accessed, already possible with DynamicMethod though.
|
Background and motivation
DynamicMethod allows accessing non public elements with its
restrictedSkipVisibility
constructor parameter, but using AssemblyBuilder and emitting complete types this way does not provide such functionality.Accessing such elements can be useful when working with runtime generation for example in serialization scenarios.
There exists a non documented
IgnoreAccessChecksToAttribute
attribute today, runtime users can't however rely on it being always supported due to being undocumented.API Proposal
API Usage
Alternative Designs
Document and expose the hidden
IgnoreAccessChecksToAttribute
.Risks
Accessing members that shouldn't be accessed, already possible with DynamicMethod though.
The text was updated successfully, but these errors were encountered: