Recently I have been exploring using the fancy new dynamic keyword introduced in C# 4.0 (DynamicObject, ExpandoObject etc..)
Now I can identify several uses (and several pitfalls) for the new introduction, but for the purpose of my project, and for this question – I am looking into cleaning up my late-bound string-based indexed collections to make them more safe.
So, before dynamics I had:
Car car1 = CARWarehouse1["Honda"];
And WITH dynamics I can now do (where CARWarehouse1 now extends the DynamicObjects base)
Car car1 = CARWarehouse1.Honda
This is all well and good, and from a quick glance it looks like I have achieved what I wanted.. but have I?
Because lets face it, for either method at compile time should I have mistyped my referenced car type, the compiler won’t have a problem, but I will run into issues at runtime.
Therefore, what I really need is compile time property checking, and Intellisense support. So – can I do it?
I know that there are libraries out there such as Moq that have this capability, and Resharper gives some sort of support within intellisense, but I’m looking for a more rounded solution.
Any ideas, big, small, easy or complex are welcome.
Nope, no such thing for dynamic types.
Basically the compiler just skips all checks on variables marked as
dynamicbecause there is no way to check anything. Properties on dynamic objects may be created at runtime, so the code can’t be checked at compile time for accuracy.