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

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

问题描述

要实现方法缺失-semantics等等在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?

特别是,你应该如何处理参数-parameter?

Especially, how exactly are you supposed to handle the "parameter"-parameter?

推荐答案

答案是,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月底的快照中获取的。对于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天全站免登陆