Brandon Cuff
Brandon Cuff

Reputation: 1478

Is it possible to skip visibility checks when generating dynamic IL with MethodBuilder's?

When generating IL using DynamicMethod it's possible to call methods and access fields that would be otherwise un-accessible if you provide 'true' for the restrictedSkipVisibility parameter in the DynamicMethod constructor

I would prefer to emit dynamic IL into a dynamic assembly instead so I can save the generated IL into an assembly at build time. If I use this method I have to use a MethodBuilder instead of a DynamicMethod. However I need to be able to skip visibility checks so I don't get MethodAccessException's when I run my dynamic code. Is there a way to do this and if so how?

Upvotes: 13

Views: 1389

Answers (2)

Tony THONG
Tony THONG

Reputation: 772

if you need to materialize your generated code by interface instead of delegate, il will be usefull to skipVisibilityCheck for a Methodbuilder. I did not find a way to do it directly, but you can simple call a DynamicMethod in your MethodBuilder by using EmitCalli(OpCodes.Calli...).

Upvotes: 0

Laurent Etiemble
Laurent Etiemble

Reputation: 27929

When using MethodBuilder into a dynamic assemblies, you are bound to the same rules as the compiler generated assemblies. So, inter-assembly visibility is governed by:

Please read the documentation and the samples, to see if it could fit your needs.

Upvotes: 3

Related Questions