借助.NET 4.0中的新动态功能,似乎应该可以动态实现接口,例如:
public interface IFoo
{
string Bar(int baz);
}
public class Foo : IFoo
{
public string Bar(int baz) { return baz.ToString(); }
}
public class Proxy : IDynamicMetaObjectProvider
{
private readonly object target;
public Proxy(object target) { this.target = target; }
// something clever goes here
}
Run Code Online (Sandbox Code Playgroud)
然后我希望有一些方法可以写:
dynamic proxy = new Proxy(new Foo());
IFoo fooProxy = (IFoo)proxy; // because the target object implements it
string bar = fooProxy.Bar(123); // delegates through to the target implementation
Run Code Online (Sandbox Code Playgroud)
但是,到目前为止,我还不确定要替换什么// …