C#4.0 动态对象是否有一些用于鸭子类型的功能?

发布于 2024-07-08 11:42:56 字数 445 浏览 12 评论 0原文

Anders 表示,在 C#4.0 中,我们将获得动态类型,或者“静态类型是动态的”对象。 这将允许任何方法调用解析发生在运行时而不是编译时。 但是是否有设施将动态对象绑定到某种合同(从而也获得完整的智能感知),而不是允许对其进行任何调用,即使您知道这不太可能有效。

即,不仅仅是

dynamic foo = GetSomeDynamicObject();

能够转换或转换它以将其限制为已知的契约,例如,

IFoo foo2 = foo.To<IFoo>;

甚至只是

IFoo foo2 = foo as IFoo;

在 C#4.0 的现有材料中找不到类似的内容,但它似乎是 C#4.0 的逻辑扩展动态范式。 有人有更多信息吗?

In C#4.0 we're going to get dynamic types, or objects whose "static type is dynamic", according to Anders. This will allow any method invocation resolution to happen at runtime rather than compile time. But will there be facility to bind the dynamic object to some sort of contract (and thereby also get full intellisense for it back), rather than allowing any call on it even if you know that is not likely to be valid.

I.e. instead of just

dynamic foo = GetSomeDynamicObject();

have the ability to cast or transform it to constrain it to a known contract, such as

IFoo foo2 = foo.To<IFoo>;

or even just

IFoo foo2 = foo as IFoo;

Can't find anything like that in the existing materials for C#4.0, but it seems like a logical extension of the dynamic paradigm. Anyone with more info?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往事随风而去 2024-07-15 11:42:56

恐怕我不知道有什么真正类似于鸭子打字的东西。 我已经 在博客中讲述了这个想法,但我不希望得到任何支持。 使用 Reflection.Emit 来创建一个类,该类将生成任何给定接口的实现,在构造函数中获取动态对象并仅代理对它的每个调用,这可能不会太难。 并不理想,但这可能是权宜之计。

I'm not aware of anything really resembling duck typing, I'm afraid. I've blogged about the idea, but I don't expect any support. It probably wouldn't be too hard to use Reflection.Emit to make a class which will generate an implementation of any given interface, taking a dynamic object in the constructor and just proxying each call through to it. Not ideal, but it might be a stopgap.

尤怨 2024-07-15 11:42:56

这是一个很酷的主意。
如果我理解你的意思,你正在描述/提出 CLR 的功能,即当你尝试将动态对象转换为接口时,它应该查看动态对象支持哪些方法/属性,并查看它是否具有有效地实现该接口。 然后,CLR 将负责在对象上“实现 IFoo”,因此您可以将动态对象强制转换为 IFoo。
几乎可以肯定这不会得到支持,但这是一个有趣的想法。

That's a cool idea.
If I understand you, you're describing/proposing a capability of the CLR, whereby, when you try and cast a dynamic object to an interface, it should look at what methods/properties the dynamic object supports and see if it has ones that effectively implement that interface. Then the CLR would take care of 'implementing IFoo' on the object, so you can then cast the dynamic object to an IFoo.
Almost certain that that will not be supported, but it's a interesting idea.

素罗衫 2024-07-15 11:42:56

Tobias Hertkorn 在此处回答了我的问题,并提供了他的< a href="http://saftsack.fs.uni-bayreuth.de/~dun3/archives/first-look-ducktyping-c-4-0-idynamicobject-metaobject/202.html" rel="nofollow noreferrer">博客文章展示了如何使用 MetaObject 上的 Convert() 方法返回动态代理的示例。 看起来非常有希望。

Tobias Hertkorn answered my question here with a link to his blogpost showing an example of how to use the Convert() method on MetaObject to return a dynamic proxy. It looks very promising.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文