I'm having an issue with cross-assembly / friend assembly type visibility.
I have the following program (which I sign / strong-name). It tells Castle DynamicProxy (I'm using version 4.2.1 of the Castle.Core
NuGet package) to create a proxy for my interface IFoo
. I'm also specifying that my internal class InterfaceProxyBase
should be the base class for the proxy type.
DynamicProxy then uses System.Reflection.Emit
to create the proxy type. But apparently, System.Reflection.Emit.TypeBuilder
does not have access to InterfaceProxyBase
.
// [assembly: InternalsVisibleTo("?")]
// ^^^
// What do I need here for my program to work both on the .NET Framework 4.5+
// and on .NET Core / .NET Standard 1.3+?
using Castle.DynamicProxy;
class Program
{
static void Main()
{
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions
{
BaseTypeForInterfaceProxy = typeof(InterfaceProxyBase) // <--
};
var proxy = generator.CreateInterfaceProxyWithoutTarget(
typeof(IFoo),
options,
new Interceptor());
}
}
public interface IFoo { }
internal abstract class InterfaceProxyBase { }
internal sealed class Interceptor : IInterceptor
{
public void Intercept(IInvocation invocation) { }
}
Unhandled Exception: System.TypeLoadException: Access is denied: 'InterfaceProxyBase'.
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
...
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
at Program.Main() in Program.cs
So, apparently I need an [assembly: InternalsVisibleTo]
attribute for the framework's own assembly / assemblies. My program (a class library, actually) targets both .NET 4.5 and .NET Standard 1.3.
Which [assembly: InternalsVisibleTo]
attribute(s) do I need (including the precise public keys) to make my code work for the mentioned platforms / targets?
P.S.: I know that I can circumvent the problem by just making InterfaceProxyBase
public and hiding it with [EditorBrowsable(Never)]
for appearance's sake, but I really don't want to make this internal type public if I don't have to.
P.P.S.: If making internals public to framework assemblies is a really bad idea, security-wise, please let me know, then I'll happily reconsider my approach.
TypeBuilder
inside the framework; not by Castle. I would've expected permissions to bubble down from Castle into the framework (those peskyLinkDemand
thingies I never really understood), but apparently that's not how it works these days. – Zoophilousmscorlib
for .NET Framework, andSystem.Reflection.Emit
for .NET Core, learn.microsoft.com/en-us/dotnet/api/… But I wonder if Castle offers you a better way. Can you try to check howTypeBuilder
is used in Castle (from the exception call stack)? That might give you some hints. – Letishaletitiamscorlib
,System
,System.Core
, andSystem.Reflection.Emit
(on .NET Core), the difficulty is knowing which public key(s) to include. – Zoophilous