Does C# 4.0’s ExpandoObject support Prototype-based inheritance? If not, why not (was it by design?) and how could this be implemented? If yes, how does it work and what differences are there compared to the way it works in Javascript?
Does C# 4.0’s ExpandoObject support Prototype-based inheritance ? If not, why not (was it
Share
First off, note that the ExpandoObject class has nothing whatsoever to do with C# 4.0. The C# team did not design or implement this object. C# 4.0 and the ExpandoObject class merely happen to both ship with the latest version of .NET.
To answer your question I refer you to the documentation for ExpandoObject, which clearly states:
As the documentation states, if you want custom dispatch semantics above mere invoking of members then use the DynamicObject class.
Someone might want an expando object but that person might neither want nor need prototype inheritance. Expando objects do not logically require any form of inheritance.
Use the DynamicObject object. Write your own prototype inheritance mechanism.
If you are attempting to write your own prototype inheritance that is exactly like JScript’s, I encourage you to read the ECMAScript specification extremely carefully. Prototype inheritance looks simple, but there are subtleties that most people get wrong. For example, even JScript experts often get this little puzzle wrong. What does this JScript code print?
Prototype inheritance does not always work the way you think it does! For an explanation of what this prints and why, see my article on the subject.