ExpandoObject、DynamicObject 和动态之间的区别
System.Dynamic.ExpandoObject
、System.Dynamic.DynamicObject
和 dynamic
之间有什么区别?
您在什么情况下使用这些类型?
What are the differences between System.Dynamic.ExpandoObject
, System.Dynamic.DynamicObject
and dynamic
?
In which situations do you use these types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
dynamic
关键字用于声明应后期绑定的变量。如果您想使用后期绑定,对于任何真实或想象的类型,您可以使用
dynamic
关键字,编译器会完成剩下的工作。当您使用
dynamic
关键字与普通实例交互时,DLR 对实例的正常方法执行后期绑定调用。IDynamicMetaObjectProvider
接口允许一个控制其后期绑定行为的类。当您使用
dynamic
关键字与IDynamicMetaObjectProvider
实现交互时,DLR 会调用IDynamicMetaObjectProvider
方法,并且对象本身决定要执行的操作。ExpandoObject
和DynamicObject
类是IDynamicMetaObjectProvider
的实现。ExpandoObject
是一个简单的类,它允许您向实例添加成员并动态
联合使用它们。DynamicObject
是一种更高级的实现,可以继承它以轻松提供自定义行为。The
dynamic
keyword is used to declare variables that should be late-bound.If you want to use late binding, for any real or imagined type, you use the
dynamic
keyword and the compiler does the rest.When you use the
dynamic
keyword to interact with a normal instance, the DLR performs late-bound calls to the instance's normal methods.The
IDynamicMetaObjectProvider
interface allows a class to take control of its late-bound behavior.When you use the
dynamic
keyword to interact with anIDynamicMetaObjectProvider
implementation, the DLR calls theIDynamicMetaObjectProvider
methods and the object itself decides what to do.The
ExpandoObject
andDynamicObject
classes are implementations ofIDynamicMetaObjectProvider
.ExpandoObject
is a simple class which allows you to add members to an instance and use themdynamic
ally.DynamicObject
is a more advanced implementation which can be inherited to easily provide customized behavior.我将尝试为这个问题提供更清晰的答案,以清楚地解释动态、ExpandoObject 和 DynamicObject 之间的区别。
很快,
dynamic
就是一个关键字。它本身不是一种类型。它是一个关键字,告诉编译器在设计时忽略静态类型检查,而在运行时使用后期绑定。因此,在本答案的其余部分中,我们不会在动态
上花费太多时间。ExpandoObject
和DynamicObject
确实是类型。从表面上看,它们看起来非常相似。这两个类都实现了IDynamicMetaObjectProvider
。然而,深入挖掘,你会发现它们根本不相似。DynamicObject 是
IDynamicMetaObjectProvider
的部分实现,纯粹是作为开发人员实现自己的自定义类型的起点,支持动态分派以及自定义底层存储和检索行为,以使动态分派发挥作用。简而言之,当您想要创建可以与 DLR 一起使用的自己的类型并使用您想要的任何自定义行为时,请使用 DynamicObject。
示例:假设您希望有一个动态类型,每当尝试对不存在的成员(即尚未在运行时添加)进行 get 操作时,该类型都会返回自定义默认值。默认值会说:“对不起,这个罐子里没有饼干!”。如果您想要一个具有类似行为的动态对象,则需要控制未找到字段时发生的情况。 ExpandoObject 不会让你这样做。因此,您需要创建具有独特动态成员解析(调度)行为的自己的类型,并使用它来代替现成的
ExpandoObject
。您可以按如下方式创建一个类型:(注意,下面的代码仅用于说明,可能无法运行。要了解如何正确使用 DynamicObject,其他地方有很多文章和教程。)
现在,我们可以使用这个虚构的类刚刚创建为动态类型,如果该字段不存在,该类型具有非常自定义的行为。
ExpandoObject
是IDynamicMetaObjectProvider
的完整实现,其中 .NET Framework 团队已为您做出所有这些决策。如果您不需要任何自定义行为,并且您认为 ExpandoObject 对您来说足够好(90% 的情况下,ExpandoObject
就足够好),那么这非常有用。例如,请参阅以下内容,对于 ExpandoObject,设计者选择在动态成员不存在时抛出异常。总而言之,
ExpandoObject
只是一种预先选择的方法,用于通过某些动态调度行为来扩展 DynamicObject,这些行为可能适合您,但可能不取决于您的特定需求。而
DyanmicObject
是一个帮助器 BaseType,它使您可以简单轻松地实现具有独特动态行为的自己的类型。一个有用的教程,上面的大部分示例源代码都是基于该教程的。
I will try to provide a clearer answer to this question, to explain clearly what the differences are between dynamic,
ExpandoObject
andDynamicObject
.Very quickly,
dynamic
is a keyword. It is not a type per-se. It is a keyword that tells the compiler to ignore static type checking at design-time and instead to use late-binding at run-time. So we're not going to spend much time ondynamic
in the rest of this answer.ExpandoObject
andDynamicObject
are indeed types. On the SURFACE, they look very similar to each other. Both classes implementIDynamicMetaObjectProvider
. However, dig deeper and you'll find they're NOT similar at all.DynamicObject is a partial implementation of
IDynamicMetaObjectProvider
purely meant to be a starting point for developers to implement their own custom types supporting dynamic dispatch with custom underlying storage and retrieval behavior to make dynamic dispatch work.In short, use DynamicObject when you want to create your OWN types that can be used with the DLR and work with whatever CUSTOM behaviors you'd like.
Example: Imagine that you'd like to have a dynamic type that returns a custom default whenever a get is attempted on a member that does NOT exist (i.e. has not been added at run time). And that default will say, "I'm sorry, there are no cookies in this jar!". If you want a dynamic object that behaves like this, you'll need to control what happens when a field is not found. ExpandoObject will not let you do this. So you'll need to create your own type with unique dynamic member resolution (dispatch) behavior and use that instead of the ready-built
ExpandoObject
.You can create a type as follows: (Note, the below code is just for illustration and may not run. To learn about how to properly use DynamicObject, there are many articles and tutorials elsewhere.)
Now, we could use this imaginary class we just created as a dynamic type that has a very custom behavior if the field doesn't exist.
ExpandoObject
is a FULL implementation ofIDynamicMetaObjectProvider
, where the .NET Framework team has made all of these decisions for you. This is useful if you don't need any custom behavior, and you feel that ExpandoObject works good enough for you (90% of the time,ExpandoObject
is good enough). So for example, see the following, and that for ExpandoObject, the designers chose to throw an exception if the dynamic member does not exist.So to summarize,
ExpandoObject
is simply one pre-chosen way to extend DynamicObject with certain dynamic dispatch behaviors that will probably work for you, but may not depending on your particular needs.Whereas,
DyanmicObject
is a helper BaseType that makes implementing your own types with unique dynamic behaviors simple and easy.A useful tutorial on which much of the example source above is based.
根据 C# 语言规范
dynamic
是一种类型声明。即dynamic x
表示变量x
的类型为dynamic
。DynamicObject
是一种可以轻松实现IDynamicMetaObjectProvider
的类型,从而可以覆盖该类型的特定绑定行为。ExpandoObject
是一种行为类似于属性包的类型。即,您可以在运行时向该类型的动态实例添加属性、方法等。According to the C# language specification
dynamic
is a type declaration. I.e.dynamic x
means the variablex
has the typedynamic
.DynamicObject
is a type that makes it easy to implementIDynamicMetaObjectProvider
and thus override specific binding behavior for the type.ExpandoObject
is a type that acts like a property bag. I.e. you can add properties, methods and so forth to dynamic instances of this type at runtime.上面的
DynamicObject
示例并没有清楚地表明差异,因为它基本上实现了ExpandoObject
已经提供的功能。在下面提到的两个链接中,很明显,在
DynamicObject
的帮助下,可以保留/更改实际类型(下面使用的示例中的XElement
链接)以及更好地控制属性和方法。https ://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/
https://blogs.msdn.microsoft。 com/csharpfaq/2009/10/19/dynamic-in-c-4-0-creating-wrappers-with-dynamicobject/
The above example of
DynamicObject
does not tell the difference clearly, because it's basically implementing the functionality which is already provided byExpandoObject
.In the two links mentioned below, it is very clear that with the help of
DynamicObject
, it is possible to preserve/change the actual type (XElement
in the example used in below links) and better control on properties and methods.https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/
https://blogs.msdn.microsoft.com/csharpfaq/2009/10/19/dynamic-in-c-4-0-creating-wrappers-with-dynamicobject/