ProxyGenerator generator = new ProxyGenerator();
var interceptor = new StandardInterceptor();
MyInterfaceImpl test = (MyInterfaceImpl)generator.CreateClassProxy(typeof(MyInterfaceImpl), interceptor);
在上面的示例中,test
对象是代理对象,假设它是由第三方创建的。
我无法传递接受MyInterfaceImpl
参数的WCF操作合同,因为其类型不是MyInterfaceImpl
,而是MyInterfaceImplProxy
。
我们如何将test
对象转换为MyInterfaceImpl
类型?请帮忙。
答案 0 :(得分:2)
我找到了这个答案here。
internal static TType UnwrapProxy<TType>(TType proxy)
{
if (!ProxyUtil.IsProxy(proxy))
return proxy;
try
{
dynamic dynamicProxy = proxy;
return dynamicProxy.__target;
}
catch (RuntimeBinderException)
{
return proxy;
}
}
答案 1 :(得分:1)
将它投射到IProxyTargetAccessor,你应该好好去。见https://github.com/castleproject/Core/blob/master/src/Castle.Core/DynamicProxy/IProxyTargetAccessor.cs