Travis Parks
Travis Parks

Reputation: 8695

Dynamic Assembly/Types With ReflectionPermissionFlag.RestrictedMemberAccess

My FlatFiles project uses System.Reflection.Emit to generate deserializers/serializers at runtime to read/write CVS files, etc. Until recently, I was able to simply use DynamicMethod passing in true for the restrictedSkipVisibility constructor parameter.

New features I am working on require I build a class at runtime instead of a simple method. I was able to implement the new logic using AssemblyBuilder/ModuleBuilder/TypeBuilder/etc. However, I ran into issues whenever I needed to access internal project classes. For that, I added the [InternalsVisibleTo] attribute to my Assembly.cs file, giving visibility to my dynamic assembly.

When I look at projects like Castle.DynamicProxy.Core, I see they are relying on [InternalsVisibleTo], as well. Looking at other projects using DynamicProxy, I can see they are adding the same attribute. Anyone using these libraries must also add the attribute: example. Even then, doesn't provide access to private classes and members.

From what I read online, it sounds like skipping visibility checks simply isn't possible using dynamic assemblies. Is this true? I am just looking for a confirmation.

My research also indicates that DynamicMethod's ability to skip visibility checks only works in certain environments. In other words, FlatFiles wouldn't work in a more restrictive environment (e.g., Internet). Is that true? That might be justification to force my users to add the [InternalsVisibleTo] attribute to their projects going forward.

Upvotes: 3

Views: 150

Answers (0)

Related Questions