dynamic - Casting a C# DynamicObject to an arbitrary type -


i trying write nice consumer api convention-based config binder in c#. configmanager.getsection(...) returns object, want return dynamic object, when cast required type invokes mapper.

is there way in c#4+ create dynamic class can handle invocation upon having explicit cast applied it?

e.g.

myconfig config = (myconfig)configurationmanager.getsection("some/section"); 

in case, dynamic object configurationmanager invoked cast perform config magic (i have solved bit)

yes, dynamicobject has override-able tryconvert. when dynamicobject subclass cast (or implicitly converted assignment) tryconvert invoked, , can dynamic details invocation binder parameter before returning result.

the binder.type property provides type object must converted. example, statement (string)sampleobject in c# (ctype(sampleobject, type) in visual basic), sampleobject instance of class derived dynamicobject class, binder.type returns string type. binder.explicit property provides information kind of conversion occurs. returns true explicit conversion , false implicit conversion.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -