能否"动态"类型不同而不同安全地在泛型集合<动态>? [英] Can the "dynamic" type vary safely in a generic collection<dynamic>?

查看:121
本文介绍了能否"动态"类型不同而不同安全地在泛型集合<动态>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到<一个href="http://stackoverflow.com/questions/2174615/static-member-inheritance-in-c/2174647#2174647">this问题,我要检查的东西在我即将到来的动态的理解键入C#4。

Based on my answer to this question, I want to check something on my understanding of the upcoming dynamic type for C# 4.

在这种情况下,我们有一个重新presents在创纪录的领域从一个不知名的数据库表拉的集合。年长code(pre-.Net 4)需要这样一个集合保持型对象的项目。一个这样的预留集合优点,我想了解一下,当你改变会发生什么对象动态

In this case, we have a collection that represents fields in a record pulled from an unknown database table. Older code (pre-.Net 4) requires such a collection hold items of type Object. Merits of a such a collection aside, I'm wondering about what happens when you change Object to dynamic.

在一方面,我想到的是,因为事情的动态类型都制定了在运行时,一切都应该只是罚款,只要程序员不作任何有关特定项目所预期的类型错别字或错误集合。

On the one hand, I expect that since things for dynamic types are all worked out at runtime that everything should be just fine as long as the programmer doesn't make any typos or mistakes about the expected type of a particular item in the collection.

在另一方面,我想知道在previous句中的所有。将来自第一次动态属性被访问运行时也许缓存结果,引起使用不同类型的失败后续调用

On the other hand, I wonder about the word "all" in the previous sentence. Would the runtime perhaps cache results from the first time a dynamic property is accessed, causing subsequent calls using different types to fail?

推荐答案

下面是从山姆的博客,简单地谈缓存策略相关的位。

Here's a relevant bit from Sam's blog that talks briefly about the caching policy.

HTTP://blogs.msdn。 COM / samng /存档/ 2008/10/29 /动态功能于c.aspx

在DLR检查缓存,如果看到   定操作已被绑定   针对当前的参数集。   因此,在我们的例子中,我们会做一个类型   匹配根据1,2,和运行时   D型。如果我们有一个高速缓存命中,   那么我们就返回缓存的结果。如果   我们没有一个高速缓存命中,则   DLR检查是否接收器   一个IDynamicObject接口。这些人是   本质上反对它知道如何   利用他们自己的绑定,这样的护理   作为COM IDispatch的对象,真正动态   对象,如Ruby或Python的,   或者一些.NET对象实现   该IDynamicObject接口界面。如果是   所有这些,那么DLR调用关闭   到IDO并要求它结合   行动。

The DLR checks a cache to see if the given action has already been bound against the current set of arguments. So in our example, we would do a type match based on 1, 2, and the runtime type of d. If we have a cache hit, then we return the cached result. If we do not have a cache hit, then the DLR checks to see if the receiver is an IDynamicObject. These guys are essentially objects which know how to take care of their own binding, such as COM IDispatch objects, real dynamic objects such as Ruby or Python ones, or some .NET object that implements the IDynamicObject interface. If it is any of these, then the DLR calls off to the IDO and asks it to bind the action.

需要注意的是调用的结果   IDO到绑定是一个前pression树   重新presents的结合的结果。   如果它不是一个氧酶,然后DLR的   调用到语言粘合剂(在我们的   情况下,C#运行时绑定),绑定   操作。 C#的运行时绑定   将结合的动作,并返回   一名前pression树重新presenting的   绑定的结果。一旦步骤2或3   已经发生了,所产生的   EX pression树被合并到   缓存机制,使得任何   后续调用可以对运行   高速缓存,而不是被反弹。

Note that the result of invoking the IDO to bind is an expression tree that represents the result of the binding. If it is not an IDO, then the DLR calls into the language binder (in our case, the C# runtime binder) to bind the operation. The C# runtime binder will bind the action, and will return an expression tree representing the result of the bind. Once step 2 or 3 have happened, the resulting expression tree is merged into the caching mechanism so that any subsequent calls can run against the cache instead of being rebound.

不过,萨姆并没有提到是完全缓存未命中的政策是什么。有两个主要的高速缓存未命中的政策:(1)触发高速缓存未命中时的参数类型改变,(2)触发高速缓存未命中时的说法身份更改。

However, what Sam doesn't mention is exactly what the cache miss policy is. There are two main cache-miss policies: (1) trigger a cache miss when the argument types change, (2) trigger a cache miss when the argument identities change.

显然前者更高性能;工作了的时候,我们可以缓存仅仅根据类型是棘手。如何所有的逻辑工作将需要相当长的时间的详细注释;希望我还是Chris或山姆将在这些天中的一个做一个博客帖子。

Obviously the former is far more performant; working out when we can cache based solely on type is tricky. A detailed exegesis of how all that logic works would take rather a long time; hopefully I or Chris or Sam will do a blog post on it one of these days.

这篇关于能否&QUOT;动态&QUOT;类型不同而不同安全地在泛型集合&LT;动态&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆