Based on my answer to this question, I want to check something on my understanding of the upcoming dynamic type for C# 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.
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/archive/2008/10/29/dynamic-in-c.aspx
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.
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.