c# - Cast a dynamic variable to a given Type -
i have dynamic variable store, depending of context, object can of several types (here foo , bar)
dynamic myvar; myvar = new foo(); //or myvar = new bar(); foo , bar contains differents methods. access methods of myvar, thought possible use casts like
(foo)myvar.mymethodoffoo(); (bar)myvar.mymethodofbar(); but it's not working, (dynamic expression) operation resolved @ runtime in code editor.
so, how can cast dynamic object available methods , properties editor ?
thank's advance.
the cast operation ((sometype)x) has lower precedence ..
therefore, code parsed (bar)(myvar.mymethodofbar()) — cast happens after method call.
you need add parentheses:
((bar)myvar).mymethodofbar();
Comments
Post a Comment