你如何实现C#4的IDynamicObject接口? [英] How do you implement C#4's IDynamicObject interface?

查看:450
本文介绍了你如何实现C#4的IDynamicObject接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在C#4.0中实现缺少方法的方法,你必须实现IDynamicObject:

To implement "method-missing"-semantics and such in C# 4.0, you have to implement IDynamicObject:

public interface IDynamicObject
{
  MetaObject GetMetaObject(Expression parameter);
}

据我所知,IDynamicObject实际上是DLR的一部分,因此它不是新的。

As far as I can figure out IDynamicObject is actually part of the DLR, so it is not new. But I have not been able to find much documentation on it.

这里有一些非常简单的示例实现(fx 在这里这里),但是任何人都可以指向更完整的实现或一些真实的文档?

There are some very simple example implementations out there (f.x. here and here), but could anyone point me to more complete implementations or some real documentation?

你应该处理参数参数?

推荐答案

简单的回答是MetaObject是什么负责实际生成将在调用站点运行的代码。它使用的机制是LINQ表达式树,已在DLR中增强。因此,不是从一个对象开始,而是从表示对象的表达式开始,最终需要返回一个表达式树,描述要执行的操作。

The short answer is that the MetaObject is what's responsible for actually generating the code that will be run at the call site. The mechanism that it uses for this is LINQ expression trees, which have been enhanced in the DLR. So instead of starting with an object, it starts with an expression that represents the object, and ultimately it's going to need to return an expression tree that describes the action to be taken.

当玩这个,请记住CTP中的System.Core的版本是从8月底的快照。它不是非常干净地对应任何特定的beta的IronPython。自那时起,对DLR进行了许多改变。

When playing with this, please remember that the version of System.Core in the CTP was taken from a snapshot at the end of August. It doesn't correspond very cleanly to any particular beta of IronPython. A number of changes have been made to the DLR since then.

此外,为了与CLR v2 System.Core兼容,从beta 4或beta 5开始的IronPython版本现在将系统命名空间中的所有内容重命名为Microsoft命名空间。

Also, for compatibility with the CLR v2 System.Core, releases of IronPython starting with either beta 4 or beta 5 now rename everything in that's in the System namespace to be in the Microsoft namespace instead.

这篇关于你如何实现C#4的IDynamicObject接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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