【发布时间】:2013-08-01 06:09:13
【问题描述】:
我正在尝试了解 Castle 的 DynamicProxy,我想做的是在运行时更改生成的代理的目标。
这样的……
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
IFoo foo = new Foo("Foo 1");
IFoo foo2 = new Foo("Foo 2");
foo.DoSomething("Hello!");
ProxyGenerator generator = new ProxyGenerator();
IFoo proxiedFoo = generator.CreateInterfaceProxyWithTarget<IFoo>(foo);
proxiedFoo.DoSomething("Hello proxied!");
(proxiedFoo as IChangeProxyTarget).ChangeProxyTarget(foo2); // cast results in null reference
proxiedFoo.DoSomething("Hello!");
}
}
}
我认为生成的代理会实现IChangeProxyTarget,但转换为接口会导致空引用。
如何在运行时更改生成的代理的目标?
更新正如答案中提到的,我尝试使用CreateInterfaceProxyWithTargetInterface,但我仍然无法转换为 IChangeProxyTarget 来更改目标。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
IFoo foo = new Foo("Foo 1");
IFoo foo2 = new Foo("Foo 2");
foo.DoSomething("Hello!");
ProxyGenerator generator = new ProxyGenerator();
IFoo proxiedFoo = generator.CreateInterfaceProxyWithTargetInterface<IFoo>(foo);
proxiedFoo.DoSomething("Hello proxied!");
IChangeProxyTarget changeProxyTarget = proxiedFoo as IChangeProxyTarget;
if (changeProxyTarget == null) // always null...
{
Console.WriteLine("Failed");
return;
}
changeProxyTarget.ChangeProxyTarget(foo2);
proxiedFoo.DoSomething("Hello!");
}
}
}
【问题讨论】:
-
那么你真正想解决什么问题?
-
我的用例是我有一个通用服务主机缓存 WCF 回调通道。服务主机不知道它正在缓存什么类型的服务合同。我想为实际回调创建代理,以便在给定多线程环境的情况下控制回调通道的访问和生命周期。能够按需更换目标将使我能够做到这一点。我已经用我自己的从 RealProxy 派生的类做到了这一点。使用 RealProxy,您可以获得底层代理类并在其上调用方法,就像我对 Castle 所做的那样。
-
对于投反对票的人,请解释我可以做些什么来改善这个问题。