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?
-
I suppose there's a reason behind the fact that the members in your generating assembly are marked internal.
You can either expose the necessary functionality in public methods, or you are left with only two choices: InternalsVisibleTo (as @tuinstoel mentioned in the comments section) or Reflection (using reflection you can access non public members in different assemblies).
Alex Lyman : I am unsure how I would go about using it in the case of dynamically generated assemblies. Is this even possible? -
InternalsVisibleTo
works by opening an assembly to others. So if you want to use assemblyFoo
from your generated types, you must specify the name of the generated assembly inAssemblyInfo.cs
forFoo
.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 theInternalsVisibleTo
attribute in assemblyFoo
.
0 comments:
Post a Comment