C# 4.0 中动态的目的是什么?什么是一些“整洁”的东西?它的用途?
正如标题所述...主要是用于您不确切知道方法调用将返回什么类型的情况(可能是 COM 互操作)?
Just as the title states... is it primarily for instances where you don't know precisely what type will be returned from a method call (COM interop maybe)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个我认为很好的改进的例子,它不会改变很多,但生活中的小事情会产生影响:)
http://weblogs.asp.net/gunnarpeipman/archive/2010/ 07/27/asp-net-mvc-3-new-viewmodel-is-dynamic-viewdata.aspx
Here is an example that i consider a nice improvement, it doesn't change a great deal, but it's the little things in life that make a difference :)
http://weblogs.asp.net/gunnarpeipman/archive/2010/07/27/asp-net-mvc-3-new-viewmodel-is-dynamic-viewdata.aspx
正如您提到的,它使得与动态语言的互操作变得更容易。
对我来说,这通常意味着 javascript。我最喜欢的用途之一是在服务器上使用 JSON,而不定义 DTO - 这也支持 LINQ to JSON:(
示例代码取自“好的,WCF,我们现在可以成为朋友了",并且一定要看到“wcf.codeplex.com 上对 jQuery 的 WCF 支持")
As you mention, it makes it easier to interoperate with dynamic languages.
For me, that usually means javascript. One of my favorite uses is consuming JSON on the server without defining DTOs - which also enables LINQ to JSON:
(Sample code lifted from "Okay, WCF, we can be friends now", and be sure to see "WCF support for jQuery on wcf.codeplex.com")
如果您不确切知道变量的类型,或者您希望允许用户指定要在运行时加载的类并使用 C# 反射 API 来加载它,那么您可以使用动态类型,这样用户就不会需要他们的类从任何东西派生,但只需要定义某些方法(如果没有,则会抛出异常,并从异常中打印错误以告诉用户发生了什么)。
这样做的优点是用户输入的次数更少,因此用户出错的空间也更少。
If you didn't exactly know the type of a variable, or you wanted to allow the user to specify a class to load at runtime and use the C# reflection API to load it, then you could use dynamic typing so that the user does not need their class to derive from anything, but just needs to define certain methods (an exception would be thrown if not and an error printed from the exception to tell the user what happened).
The advantage of this is that there would be less typing by the user, and therefore less space for user error.
它们在您不知道返回类型或在编译时不知道对象上可用的成员的情况下非常有用,因此您可以将其推迟到运行时。动态的另一个巧妙用法是
ExpandoObject
< /a>.这允许您在运行时向对象添加成员。我用它来替换Dictionary
样式代码。They are useful in scenarios where you don't know the return type, or you don't know at compile-time what members will be available on an object, so you can defer it until run-time. Another neat use of dynamic is
ExpandoObject
. This allows you to added members to an object at run-time. I've used it to replaceDictionary<string, object>
style code.是的,COM 互操作可能是最常见的地方,动态在其中非常有用。我将它与 IronPython 结合使用,使对 python 方法(本质上是动态脚本)的调用更加干净和容易。这些就是它的目的。
也就是说,引入动态的总体效果是使 C# 成为一种脚本语言,并且很有可能将其用于许多其他目的(这让语言纯粹主义者感到恐惧) 。例如,MVC 3 中的 ViewBag 属性只是 ViewData 集合的动态包装器。它使语法看起来有点“干净”(
ViewBag.FirstName
而不是ViewData["FirstName"]
),但代价是清晰度的潜在代价:乍一看, C# 开发人员可能会认为前者是对某种强类型对象的属性的访问,这在没有编译器错误的情况下会给他们一种错误的安全感。我还听说过一些框架可以将 JSON 字符串反序列化为动态对象,因此您的 .NET 系统可以处理来自 AJAX 请求的动态 javascript 对象,就像 javascript 处理它们一样。对于某些人来说这真的很酷,但对其他人来说至少有点令人毛骨悚然。
Yes, COM interop is probably the most common place where dynamics will be incredibly useful. I've used it in conjunction with IronPython to make calls to python methods (which are dynamic scripts by nature) much cleaner and easier. These are the kinds of things it was intended for.
That said, the overall effect of introducing dynamics is to enable C# to become a kind of scripting language, and there is a strong likelihood that it will be used for a number of other purposes along those lines (much to the horror of language purists). For example, the
ViewBag
property in MVC 3 is just a dynamic wrapper for the ViewData collection. It makes syntax look just a tad "cleaner" (ViewBag.FirstName
instead ofViewData["FirstName"]
), but at the potential cost of clarity: At a glance, a C# developer might assume that the former is an access to a property on a strongly-typed object of some kind, which can give them a false sense of security when there are no compiler errors.I've also heard of frameworks that can deserialize a JSON string into a dynamic object, so your .NET system could handle dynamic javascript objects from AJAX requests in much the same way javascript handles them. It's really cool to some people and at least a little creepy to others.
通过动态获得的“酷”东西之一是没有访问者模式的多重调度。
one of the "cool" things you get with dynamic is multiple dispatch without the visitor pattern.