动态实现接口的.NET 4.0(C#) [英] Dynamically implementing an interface in .NET 4.0 (C#)

查看:926
本文介绍了动态实现接口的.NET 4.0(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着新的动态功能,在.NET 4.0中,现在看来似乎应该是可以动态地实现一个接口,如给定:

 公共接口的IFoo
{
    串酒吧(INT巴兹);
}

公共类Foo:IFoo的
{
    公共字符串酒吧(INT巴兹){返回baz.ToString(); }
}

公共类代理:IDynamicMetaObjectProvider
{
    私人只读对象的目标;

    公共代理(对象target){this.target =目标; }

    //一些聪明到这里
}
 

然后我希望有一些方法,使人们有可能写:

 动态代理=新代理(新富());
IFoo的fooProxy =(的IFoo)代理; //因为目标对象实现了它
串吧= fooProxy.Bar(123);通过给目标执行//代表
 

不过,到目前为止,我不知该怎么替换 //一些聪明到这里

所以,我的问题是:

  1. 这是真正可以做到与动态运行?看来,实现动态之类的东西的方法和属性是相当容易的,但我没有找到任何文档关于动态实现接口和转换他们。

  2. 假设这是可能的,难的是如何它可能是? (你可以假设我是一个体面的程序员用大量的像反射的经验,但新的动态框架。)

  3. 有什么资源,这将有助于点我在正确的方向实施这样的事情?甚至样品,其中这种事情已经做了,我可以作为一个起点使用?

解决方案

据我所知,这是不可能的,无需人工干预,编写或生成code转发该接口成员包装的实例。如果你想看到这样的事情Microsoft提供的支持,你可能要考虑在投票<一href="https://connect.microsoft.com/VisualStudio/feedback/details/526307/add-automatic-generation-of-interface-implementation-via-implementing-member">https://connect.microsoft.com/VisualStudio/feedback/details/526307/add-automatic-generation-of-interface-implementation-via-implementing-member

With the new dynamic capabilities in .NET 4.0, it seems like it should be possible to dynamically implement an interface, e.g. given:

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
}

Then I'm hoping there is some way to make it possible to write:

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

But, as yet, I'm unsure what to replace // something clever goes here with.

So, my questions are:

  1. Is this actually possible to do with the dynamic runtime? It appears that dynamically implementing things like methods and properties is fairly easy, but I haven't found any documentation about dynamically implementing interfaces and conversions to them.

  2. Assuming this is possible, how difficult is it likely to be? (You can assume I'm a decent programmer with plenty of experience of things like reflection, but new to the dynamic framework.)

  3. Are there any resources which would help to point me in the right direction for implementing something like this? Or even samples where this kind of thing has already been done that I can use as a starting point?

解决方案

As far as I know, it's not possible without manual intervention to write or generate code that forwards the interface members to the wrapped instance. If you would like to see Microsoft-provided support for this sort of thing, you might want to consider voting at https://connect.microsoft.com/VisualStudio/feedback/details/526307/add-automatic-generation-of-interface-implementation-via-implementing-member .

这篇关于动态实现接口的.NET 4.0(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆