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

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -