我正在为SL v2.5.1.0使用CaSTLe Windsor。我有它代理内部类(接口(interface)是公共(public)的,但是实现是内部的,因此使用者只知道该接口(interface))。
我在我的程序集中使用以下属性的内部类
[assembly: InternalsVisibleTo("Castle.Core, PublicKey=002400000480000094000000060200000024000052534131000400000100010077F5E87030DADCCCE6902C6ADAB7A987BD69CB5819991531F560785EACFC89B6FCDDF6BB2A00743A7194E454C0273447FC6EEC36474BA8E5A3823147D214298E4F9A631B1AFEE1A51FFEAE4672D498F14B000E3D321453CDD8AC064DE7E1CF4D222B7E81F54D4FD46725370D702A05B48738CC29D09228F1AA722AE1A9CA02FB")]
[assembly: InternalsVisibleTo("Castle.Windsor, PublicKey=002400000480000094000000060200000024000052534131000400000100010077F5E87030DADCCCE6902C6ADAB7A987BD69CB5819991531F560785EACFC89B6FCDDF6BB2A00743A7194E454C0273447FC6EEC36474BA8E5A3823147D214298E4F9A631B1AFEE1A51FFEAE4672D498F14B000E3D321453CDD8AC064DE7E1CF4D222B7E81F54D4FD46725370D702A05B48738CC29D09228F1AA722AE1A9CA02FB")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
在完全.NET 4.0模式下,对于.NET 4.0 CaSTLe程序集,此方法工作正常,并且可以将我的类型代理为OK。在Silverlight中,通过Silverlight CaSTLe程序集,我得到:
Type ConsoleApplication4.MyTypeToBeProxied is not public. Can not create proxy for types that are not accessible.
另外,仅在解决问题时,添加以下内容似乎没有任何区别...:
[assembly: InternalsVisibleTo("System.Core, PublicKey=00000000000000000400000000000000")]
[assembly: InternalsVisibleTo("System.Core, PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001008d56c76f9e8649" +
"383049f383c44be0ec204181822a6c31cf5eb7ef486944d032188ea1d3920763712ccb12d75fb7" +
"7e9811149e6148e5d32fbaab37611c1878ddc19e20ef135d0cb2cff2bfec3d115810c3d9069638" +
"fe4be215dbf795861920e5ab6f7db2e2ceef136ac23d5dd2bf031700aec232f6c6b1c785b4305c" +
"123b37ab")]
并且我还在运行时验证了SL中动态托管程序集的名称实际上仍然是DynamicProxyGenAssembly2。
有任何想法吗?谢谢。
编辑:
我发现了我认为的问题:
.NET 4.0的城堡具有:
private bool IsAccessible(Type target)
{
// ....
return ((target.IsPublic || target.IsNestedPublic) || internalAndVisibleToDynProxy);
}
在DefaultProxyBuilder中...并且SL 4具有
private bool IsAccessible(Type target)
{
target.IsNested();
return (target.IsPublic || target.IsNestedPublic);
}
这是可以在CaSTLe来源中解决的吗?还是我需要/应该对DefaultProxyFactory进行子类化?
最佳答案
我对此有些运气。老实说,我不确定为什么,但是我无法重现Krzysztof描述的问题。我怀疑...也许...这与我的程序集是SN ...这一事实有关...这需要我进行其他更改...但一旦这样做,我就能够解决代理问题用于SL测试应用程序中的内部类(具有公共(public)接口(interface))。
我对CaSTLe.Core源代码所做的唯一更改是使ModuleScope.moduleBuilder
和ModuleScope.moduleBuilderWithStrongName
字段 protected ,而不是私有(private)。但是同样,这只是必要的,这样我就可以在SL中定义SN的动态程序集,而CaSTLe.Core中的ModuleScope则为SL禁用了SN的动态程序集。所以,现在我有了一个自定义的ModuleScope,如下所示:
private class StrongNameModuleScope : ModuleScope
{
public StrongNameModuleScope()
{
var assemblyName = new AssemblyName("DynamicProxyGenAssembly2");
// copied from another one of my SN assemblies (plus GetName() on assembly is security critical so I can't pull it off the executing assembly)
byte[] publicKey = Convert.FromBase64String(@"ACQAAASAAACUAAAABgIAAAAkAABSU0ExAAQAAAEAAQBvwWquPXQG9zfemS8uDsFdGDScOCSjZ9aFsQDtrrAqKzvlxEGMz3t9Q9M3X9NKqy1ouLZi+sX8yVDafX+UnygFWWfOBosw9nGwG61MTKEhEjdKH0rECahGIXY+ETdNY64HduuH/BIbEs/RDhrrH2hiqGrOGb6AghD1sZ6g0A1qkg==");
assemblyName.SetPublicKey(publicKey);
AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder module = assembly.DefineDynamicModule("DynamicProxyGenAssembly2");
moduleBuilder = module;
moduleBuilderWithStrongName = module;
}
}
和自定义的DefaultProxyBuilder:
/// <summary>
/// A custom IProxyBuilder copies from the full .NET Castle implementation that allows for proxies of internal types where the InternalsVisibleToAttribute is applied.
/// </summary>
private class DefaultProxyBuilder : IProxyBuilder
{
...
// Methods
public DefaultProxyBuilder()
: this(new StrongNameModuleScope())
{
}
...
private static bool IsAccessible(Type target)
{
bool isTargetNested = target.IsNested;
bool isNestedAndInternal = isTargetNested && (target.IsNestedAssembly || target.IsNestedFamORAssem);
bool internalAndVisibleToDynProxy = ((!target.IsVisible && !isTargetNested) || isNestedAndInternal) && InternalsHelper.IsInternalToDynamicProxy(target.Assembly);
return ((target.IsPublic || target.IsNestedPublic) || internalAndVisibleToDynProxy);
}
}
和一个自定义的DefaultProxyFactory:
/// <summary>
/// A simple DefaultProxyFactory to wrap the modified DefaultProxyBuilder.
/// </summary>
private class DefaultProxyFactory : global::Castle.Windsor.Proxy.DefaultProxyFactory
{
public DefaultProxyFactory()
{
generator = new ProxyGenerator(new DefaultProxyBuilder());
}
}
以及容器设置:
container = new WindsorContainer();
container.Kernel.ProxyFactory = new DefaultProxyFactory();
我不太喜欢不得不修改CaSTLe.Core源代码,所以我真的很想听听您的想法Krzysztof ...如果此解决方案不适用于其他测试用例,您是否可以保护这些字段? ?
关于.net - 温莎城堡内部 View 可见到Silverlight,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4156499/