C# Convert dynamic object to interface -


namespace mynamespace {     public interface imyinterface     {         string getmethod();     }     public class myclass : imyinterface     {        public string getmethod()        {}     } } 

//main program

var ruleclass = activator.createinstance(assembly.getexecutingassembly().fullname, "mynamespace.myclass");  if( ruleclass != null) {    imyinterface myclass = (imyinterface)ruleclass;    //throws exception.  } 

how can convert ruleclass imyinterface type can call specific methods in it?

use unwrap method.

imyinterface myclass = (imyinterface)ruleclass.unwrap(); 

you can create directly too

var myclass = activator.createinstance(typeof(myclass)) myclass; 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -