c# - Instantiate multiple classes from a single generic overide style class -
i looking way able instantiate multiple classes have same method.
looking can pass in name of class string. here have far while compiles not run not instantiating class correctly.
im doing know missing something. if can appreciate it.
public class usemyclass{ public runmyclass(string st1, list<string> mysts, string myclass){ classprocessor cp = new myclass1(); // works classprocessor cp = new myclass2(); // works // here want able using string variable myclass (myclass1 or myclass2 ) string value //classprocessor cp = new class(myclass); // not work null classprocessor cp = (classprocessor)system.reflection.assembly.getexecutingassembly().createinstance(myclass); cp.mymethod(st1,mysts); } }
file1: classproccessor.cs
public interface classproccessor{ public void mymethod(string st1, list<string> mysts); }
file2: myclass1.cs
public class myclass1 : classproccessor{ public void mymethod(string st1, list<string> mysts){ //do something; } }
file3: myclass2.cs
public class myclass2 : classproccessor{ void mymethod(string st1, list<string> mysts){ // different; } }
how can create new instance of class using string name of class through use of interface? know must missing simple.
interfaces don't happen automatically.
need explicitly implement interface in each class:
public clas myclass1 : iclassprocessor
Comments
Post a Comment