Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I let Reflection.Emit assemblies access internal members in the generating assembly?

For one of my projects, I need to generate at run time some classes, and I thought it would be fairly simple to do using Reflection.Emit, but I'm getting MemberAccessExceptions when I run some of the generated code that calls methods that are marked internal in the generator assembly. Is there any way to tell the runtime that the dynamic assembly should be able to access my own code directly? I would really rather not publicly expose any of these members to consumers of my library.


Regarding InternalsVisibleTo, I am unsure how I would go about using it in the case of dynamically generated assemblies. Is this even possible?
like image 259
Alex Lyman Avatar asked Dec 03 '25 22:12

Alex Lyman


1 Answers

InternalsVisibleTo works by opening an assembly to others. So if you want to use assembly Foo from your generated types, you must specify the name of the generated assembly in AssemblyInfo.cs for Foo.

If you're emitting a new assembly using the AssemblyBuilder class, you can specify the name for the generated assembly. This name has to match the name used for the InternalsVisibleTo attribute in assembly Foo.

like image 175
Brian Rasmussen Avatar answered Dec 05 '25 13:12

Brian Rasmussen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!