C#4:动态类型的真实世界的例子例子、类型、真实、动态

2023-09-02 21:09:58 作者:绝对嚣张

我想我有我的一半大脑缠在C#4动态类型的概念,但不能为我的生活找不出这样一个场景,我真的想使用它。

I think I have my brain halfway wrapped around the Dynamic Types concept in C# 4, but can't for the life of me figure out a scenario where I'd actually want to use it.

我敢肯定有很多,但我只是遇到问题建立连接,以我怎么可能设计一种解决方案,更符合动力学解决,而不是接口,依赖注入等。

I'm sure there are many, but I'm just having trouble making the connection as to how I could engineer a solution that is better solved with dynamics as opposed to interfaces, dependency injection, etc.

那么,什么是一个真实的应用场景,动态类型的使用是否合适?

So, what's a real-world application scenario where dynamic type usage is appropriate?

推荐答案

有很多情况下你在哪里的已的使用动态类型和动态绑定的今天的。你只是没有意识到这一点,因为它是所有背后隐藏的字符串或 System.Object的,因为直到C#4,必要的支持是不存在。

There are lots of cases where you are already using dynamic typing and dynamic binding today. You just don't realize it, because it is all hidden behind strings or System.Object, since until C# 4, the necessary support wasn't there.

一个例子是COM互操作:COM实际上是一个半动态对象系统。当你做COM互操作,有很多方法实际上返回一个动态的对象,但因为C#不支持他们,他们返回 System.Object的,你不得不投他们自己,可能追赶的道路上异常。

One example is COM interop: COM is actually a semi-dynamic object system. When you do COM interop, a lot of methods actually return a dynamic object, but because C# didn't support them, they were returned as System.Object and you had to cast them yourself, possibly catching exceptions on the way.

另一个例子是交互与动态类型(甚至无类型)的数据,如JSON,CSV,HTML,XML的无模式,无模式的Web服务,无模式数据库(其中,毕竟,新的辣味)。今天,您使用的这些字符串。一个XML API看起来像

Another example is interacting with dynamically typed (or even untyped) data, such as JSON, CSV, HTML, schemaless XML, schemaless web services, schemaless databases (which are, after all, the new hotness). Today, you use strings for those. An XML API would look like

var doc = new XmlDocument("/path/to/file.xml");
var baz = doc.GetElement("foo").GetElement("qux");

等。但是怎么样:

and so on. But how about:

dynamic doc = new XmlDocument("/path/to/file.xml");
var baz = doc.foo.qux;

那岂不是好看?

Doesn't that look nice?

第三个例子是反思。如今,通过反射的方法调用是通过传递一个字符串 InvokeMember 进行(或东西叫什么)。那岂不是更好,你知道的,只是调用该死的东西?

A third example is reflection. Today, invocation of a method via reflection is done by passing a string to InvokeMember (or whatever the thing is called). Wouldn't it be nicer to, you know, just invoke the damn thing?

然后,有动态数据(第二实例的基本上相反)的代的。这里有一个例子,如何生成一些动态XML:

Then, there is generation of dynamic data (basically the opposite of the second example). Here's an example how to generate some dynamic XML:

dynamic doc = new XmlBuilder();
doc.articles(id=42, type="List", () => {
  article(() => {
    number(42);
    title("blahblubb");});});

这是不是几乎一样美丽相当于红宝石,但它是最好的,我能想出在如此短的时间: - )

This is not nearly as beautiful as the equivalent Ruby, but it is the best I could come up with at such short notice :-)

最后但并非最不重要的,具有动态类型语言的集成。无论是在JavaScript的Silverlight应用程序,自定义规则引擎嵌入在您的业务应用程序,或者你在你的CAD程序/ IDE /文本编辑器托管DLR实例。

And last but certainly not least, integration with a dynamically typed language. Whether that is JavaScript in a Silverlight application, a custom rules engine embedded in your business app or a DLR instance that you host in your CAD program/IDE/text editor.