title: Dynamic Assembly/Types With ReflectionPermissionFlag.RestrictedMemberAccess
body: <p>My <a href="https://github.com/jehugaleahsa/FlatFiles" rel="nofollow noreferrer">FlatFiles</a> project uses <code>System.Reflection.Emit</code> to generate deserializers/serializers at runtime to read/write CVS files, etc. Until recently, I was able to simply use <code>DynamicMethod</code> passing in <code>true</code> for the <code>restrictedSkipVisibility</code> <a href="https://msdn.microsoft.com/en-us/library/bb348332(v=vs.110).aspx" rel="nofollow noreferrer">constructor parameter</a>.</p>
  
  <p>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 <code>AssemblyBuilder</code>/<code>ModuleBuilder</code>/<code>TypeBuilder</code>/etc. However, I ran into issues whenever I needed to access <code>internal</code> project classes. For that, I added the <a href="https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx" rel="nofollow noreferrer">[InternalsVisibleTo]</a> attribute to my <code>Assembly.cs</code> file, giving visibility to my dynamic assembly.</p>
  
  <p>When I look at projects like <a href="https://github.com/castleproject/Core" rel="nofollow noreferrer">Castle.DynamicProxy.Core</a>, I see they are relying on <code>[InternalsVisibleTo]</code>, as well. Looking at other projects <em>using</em> DynamicProxy, I can see they are adding the same attribute. Anyone using these libraries must also add the attribute: <a href="https://stackoverflow.com/questions/17569746/mocking-internal-classes-with-moq-for-unit-testing">example</a>. Even then, doesn't provide access to <code>private</code> classes and members.</p>
  
  <p>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.</p>
  
  <p>My research also indicates that <code>DynamicMethod</code>'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 <code>[InternalsVisibleTo]</code> attribute to their projects going forward.</p>