不再有动态的、尴尬的反思了吗?
C# 4.0 引入了 dynamic
关键字,它将在运行时查找。
这是否意味着我们不再需要尴尬的反思? 如果是的话,你能举个例子吗?
C# 4.0 introduces dynamic
keyword, which will look up at run-time.
Does this mean we'll need awkward reflection no more? If does, Can you show up an example of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们仍然会使用反射 - 对常规 CLR 对象使用“动态”将调用基于反射的调度程序。
所以 - 我们仍然会有反射,但它会更容易做到。
这是一个例子:
我不了解你,但我喜欢前者。 =)
在这两种情况下,我都没有得到编译时检查、没有 Intellisense、没有 IDE 支持 - 但前一种情况比后一种情况更具表现力。
We'll still have Reflection - using 'dynamic' against regular CLR objects will invoke a Reflection-based dispatcher.
So - we'll still have Reflection, but it'll be easier to do.
Here's an example:
I don't know about you, but I like the former. =)
In both these cases, I get no compile-time checking, no Intellisense, no IDE support - but the former case is much more expressive than the latter.
动态调度只是反射的一种可能用途。 有很多充分的理由询问类的结构,获取有关该结构的信息并以某种形式可视化或以某种方式对其进行操作,而无需动态访问成员。 反思将继续存在。 :)
如果您想要动态关键字的示例,这里有一段来自 PDC 的视频 < a href="http://www.microsoft.com/presspass/exec/techfellow/hejlsberg/default.mspx" rel="nofollow noreferrer">该人本人谈论它(以及其他与 C# 4.0 相关的内容) )。
Dynamic dispatch is only one possible use of Reflection. There are many good reasons to interrogate a class for its structure, get information about that structure and visualize in some form or act on it in some way without ever dynamically accessing members. Reflection is here to stay. :)
If you want examples of the dynamic keyword, here is a video from PDC of the man himself talking about it (and other stuff C# 4.0 related).
动态将大大有助于解决仅通过名称已知的方法的问题,其中该名称是已知的并在编译时固定 - 但当然,如果您控制类型,此类方法也可以表示为接口。
在某些情况下,动态根本没有帮助:
最大的用途是对于
动态
,请参阅:但它绝对不能解决所有反射问题。
Dynamic will go a long way to solving problems with methods known only by name, where that name is known and fixed at compile time - but of course, such methods could also be expressed as interfaces if you control the types.
There are cases where
dynamic
would not help at all:The biggest uses I see for
dynamic
are:But it definitely doesn't solve every reflection woe.