C
C#2mo ago
JohnAustin

Accessing private methods when using Reflection.Emit and AssemblyBuilder

I am writing a compiler that runs in process, and generates IL on the fly to speed up various execution graphs in our code. For convenience we allow users to call private static functions within the generated code. I have been using DynamicMethod to emit IL, which allowed emitting calls to private methods. However, after swapping over to a more standard AssemblyBuilder approach, I'm getting MethodAccessExceptions in the generated IL code. Are emitted dynamic assemblies more strict than DynamicMethods? It seems like an arbitrary distinction, as both are emitted into the same domain, and not written to disk. Is there a good way to get around MethodAccessException, or do I have to stick with DynamicMethod only?
2 Replies
Petris
Petris2mo ago
GitHub
[API Proposal]: Add support for skipping visibility checks with Ass...
Background and motivation DynamicMethod allows accessing non public elements with its restrictedSkipVisibility constructor parameter, but using AssemblyBuilder and emitting complete types this way ...
JohnAustin
JohnAustin2mo ago
Oh interesting. Unfortunately I am stuck on Mono / Unity. But this sent me down the right path. Seems like there might be support for SecurityPermissionAttribute or IgnoreAccessChecksToAttribute in Mono.