动态关键字是否“仅”与动态语言一起使用?
我最近参加了 Code Camp 12,那里的一位演讲者说 C# 4.0 中的新 dynamic
关键字应该仅用于与动态语言的互操作。我认为他还说过,与正常反射(本身有点慢)相比,它有点慢。
但后来我听到 Scott Hanselman 提到动态关键字“让反思不那么痛苦”。
那么使用dynamic关键字来反映不是来自动态代码的对象是可以接受的吗?
I attended Code Camp 12 recently, and a speaker there said that the new dynamic
keyword in C# 4.0 should only be used for interopping with dynamic languages. I think he also said that it is somewhat slow, compared to normal reflection (which itself is somewhat slow).
But then I heard Scott Hanselman mention that the dynamic keyword "makes reflection less painful".
So is it going to be acceptable to use the dynamic keyword for the purpose of reflecting an object that doesn't come from dynamic code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会说“不”,但不要开始疯狂地使用它。实际上,根据我的基准测试,
动态
比基本反射更快,因为它保留委托(而不是使用反射Invoke
所有时间)。特别是,有两个优点:MakeGenericMethod
等非常痛苦)符 但是,可以使用接口等方法来完成您需要的操作;非动态类型上的
dynamic
实际上相当于鸭子类型。这在非常有限的一组场景中非常有用;主要是:接口是首选。不要排除它们。动态的缺点是要有用(无需编写疯狂的代码),您需要在编译时知道名称;但事实往往并非如此,否则我们就不会陷入困境!当您在运行时只知道名称时,还有其他选项(
Expression
、Delegate.CreateDelegate
、“HyperDescriptor”、DynamicMethod
等)快速访问数据。I would say "no", but don't start using it insanely. Actually,
dynamic
is, from what I've benchmarked, faster than basic reflection, since it is keeping delegates (rather than using reflectionInvoke
all the time). In particular, two strengths are:MakeGenericMethod
etc is just so painful)However, there are using ways of doing what you need with interfaces etc;
dynamic
on a non-dynamic type really amounts to duck-typing. This is useful in a very limited set of scenarios; mostly: interfaces would be preferred. By don't rule them out.The downside of
dynamic
is that to be useful (without writing insane code) you need to know the names at compile-time; which often isn't the case, or we wouldn't be in this pickle! When you only know the name at runtime, there are other options (Expression
,Delegate.CreateDelegate
, "HyperDescriptor",DynamicMethod
, etc) of access the data in a fast way.如果您觉得需要鸭子类型并且并不真正需要编译时类型安全,请继续使用动态。我确信它会在纯 C# 代码中出现许多新用法(例如使用 ExpandoObject 查询 XML 等动态数据源)。我确信大量的新用法将是多余的,就像大量使用泛型只是表达多态性的更复杂的方式一样。
关于性能,.NET 4 中的 DLR 正在尝试使动态类型变得“快速”。与往常一样,当您开始分析应用程序时,您就会发现它是否足够快。
If you feel like you need duck typing and don't really need compile time type safety, go ahead and use dynamic. I'm sure there will be a number of new usages for it popping up in C# only code (such as querying a dynamic data source such as XML using the ExpandoObject). I'm just as sure a large number of the new usages will be superfluous the same way much use of generics is just a more complex way of expressing polymorphism.
Regarding performance, the DLR in .NET 4 is attempting to make dynamic typing "fast". As always, if it's fast enough is something you'll figure out when you start to profile your application.
据我所知,在 C# 中引入
dynamic
关键字的主要原因是为了更容易与 COM 对象进行互操作。但当然,它可以用于反射......From what I know, the main reason why the
dynamic
keyword was introduced in C# was to make it easier to interoperate with COM objects. But of course, it can be used for reflection...答案是,dynamic 关键字是用于互操作的,但不仅仅用于动态语言互操作。 COM 互操作只是一个示例。 C# 团队已经修改了 COM 互操作来使用此功能,这使得互操作变得更加容易。我最近看到 ASP.NET MVC 视图正在做类似的事情。
我还发布了一个示例,显示动态关键字的另一个用例:
C# 4.0 中的动态:使用 DynamicObject 创建包装器。这些正是 Freed 所讨论的示例类型:简化与 XML 数据的互操作。
Well, the answer is that the dynamic keyword is for interop, but not only for dynamic languages interop. COM interop is just one example. C# team has already modified COM interop to use this feature and this made the interop much easier. I've recently seen that ASP.NET MVC Views are doing something similar.
I have also posted an example that shows another use case for dynamic keyword:
Dynamic in C# 4.0: Creating Wrappers with DynamicObject. These are exactly the types of examples Freed was talking about: simplifying interop with XML data.
我不得不说,“不”。看看 http://haacked.com/ archive/2009/08/26/method-missing-csharp-4.aspx 是一个不太预期的使用示例。
显然,dynamic 关键字的目的是使 COM 和动态语言更易于使用,但这并不意味着我们应该将其限制在这些领域。就性能而言:牢记这一点,但在出现性能问题之前不要关注它。 (这是那些较低级别的细节之一,不太可能影响高级设计选择,而高级设计选择可能会在开始之前就削弱性能。)
编辑
此外,任何优秀的语言设计者都知道这样一个事实:人们会以意想不到的方式使用语言功能。这与本次讨论特别相关,因为他们制作了一个支持动态行为的界面。他们有目的地允许人们将几乎任何东西挂接到“动态”关键字功能中。
I would have to say, "No". Take a look at http://haacked.com/archive/2009/08/26/method-missing-csharp-4.aspx for an example of a less-expected use.
The dynamic keyword was clearly intended to make COM and dynamic languages much easier to work with, but that doesn't mean we should limit it to those areas. As far as performance goes: keep it in mind, but don't focus on it until you have a performance problem. (This is one of those lower level details that is quite unlikely to affect high level design choices that can cripple performance before you even start.)
Edit
Also, any good language designer is aware of the fact that people will use the language features in unexpected ways. This is particularly relevant for this discussion, because they made an interface that enables the dynamic behavior. They purposefully allowed people to hook pretty much anything into the "dynamic" keywords capabilities.