An object can provide its binding semantics by implementing
IDynamicMetaObject Provider—or more easily by subclassing
DynamicObject, which provides a default implementation of this
interface.
I never implemented this interface nor class and always been able to execute things dynamically:
public class aa
{
public void bbb()
{ }
}
dynamic a = new aa().bbb();
so what do they mean by this quote ?
im trying to understand when should i need to use IDynamicMetaObject or the DynamicObject inheritance
The
dynamickeyword causes references to an object to be late-bound and only resolved at runtime. Usually, the reason for this is because you are using anIDynamicMetaObjectProviderimplementer, which allows your code to decide how to treat the run-time property resolution. That is, you may want to add and remove properties at runtime (typically implemented as a dictionary, likeExpandoObject).What your example code is doing is just preventing compile-type type checking. In fact it’s worse than doing nothing, because if you wrote code that accessed a nonexistent property or method of your class, it would still compile, but of course crash at runtime. I can’t think of many good reasons to refer to concrete classes with
dynamic(expect for squishy typecasting or duck-typing, but we usually use interfaces for stuff like this in C#!)Example implementation of a dynamic object that inherits
DynamicObjectfrom one of my own projects:https://github.com/jamietre/IQObjectMapper/blob/master/source/IQObjectMapper/IQDynamicObject.cs
(It makes one call to a factory method that returns a new dictionary with options, all you need to do to make this non-dependent is change the constructor from:
to:
and get rid of a couple conditons that check options