1

I am looking for a way to be able to instantiate multiple classes that have the same method.

looking to do something where I can pass in the name of the class as a string. Here is what I have so far while it compiles it will not run as I am not instantiating the class correctly.

Im doing something like this but I know I am missing something. If anyone can help I would appreciate it.

public class useMyClass{
   public runMyClass(string st1, list<string> mysts, string myClass){
       classProcessor cp = new myClass1(); // this works
       classProcessor cp = new myClass2(); // this works

       // Here I just want to be able to do using the string variable myClass (myClass1 or myClass2 ) could be the string value
       //classProcessor cp = new Class(myClass);

       // This does not work always 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){
        // do something different;
    }
}

How can I create a new instance of a class using the string name of the class through the use of the interface? I know I must be missing something simple.

Taryn East
  • 27,486
  • 9
  • 86
  • 108
  • What error do you get? – SLaks Apr 19 '13 at 20:54
  • When I run through the program i get a null reference exception classProcessor cp = (classProcessor)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(myClass); // makes it past here cp.myMethod(st1,mysts); // says null reference exception use "new" keyword – user2300516 Apr 20 '13 at 03:07

1 Answers1

0

Interfaces don't happen automatically.
You need to explicitly implement the interface in each class:

public clas MyClass1 : IClassProcessor
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • with this added on the class for my interface, I still get a null return instead of it instantiating a new class – user2300516 Apr 19 '13 at 22:22
  • @user2300516: Then your type name wasn't found. Make sure it includes the namespace, and that you're looking in the correct assembly. – SLaks Apr 21 '13 at 01:31
  • I have all of these files living in the same name space, and they all have the same using libraries. – user2300516 Apr 21 '13 at 12:52
  • @user2300516: It doesn't matter. You must pass the full namespace to `CreateInstance()`. – SLaks Apr 21 '13 at 13:07
  • I plugged into the full name space "MyNameSpace.Processors.MyClass1" and still null. The orginating call comes from a webservice to this library, then I am doing this portion of the interface to classes, but maybe the assembly im just not sure – user2300516 Apr 21 '13 at 14:40
  • classProcessor cp = (classProcessor)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("MyNameSpace.process.myClass1"); – user2300516 Apr 21 '13 at 16:54
  • @user2300516: Are they defined in the same assembly? – SLaks Apr 21 '13 at 16:57
  • I am guessing so, I started with a webapplication, then added a webservice to it. I then added a new project to that which created a new solution. The webservice portion does have a different namespace but all the classes besides the webservice are in the second namespace/project. The webservice just makes the call to ActionProcessor. which is everything posted here – user2300516 Apr 21 '13 at 18:23
  • @user2300516: You need to start from the assembly that contains the type. (eg, `typeof(Whatever).Assembly`) – SLaks Apr 21 '13 at 19:20
  • I have the interface so it will call any of the class directly. – user2300516 Apr 30 '13 at 16:42
  • I know I can do IClassProcessor icp = new MyClass1; but I want to do this where the class name is a string value string strNewClassName = "MyClass1"; IClassProcessor icp = new strNewClassName; where this string becomes a new class – user2300516 Apr 30 '13 at 17:05
  • @user2300516: You need to start from the assembly that contains the type. (eg, `typeof(Whatever).Assembly`) – SLaks Apr 30 '13 at 19:39
  • I tried to use the typeof().Assembly.CreateInstance(strNewClassName) but I am not sure what should go into the typeof(). Library, assembly, class. – user2300516 Apr 30 '13 at 22:22
  • I was looking over this post http://stackoverflow.com/questions/5262693/c-sharp-using-activator-createinstance?answertab=active#tab-top am I missing something here or can I do this with out the use of these Factories like I am. – user2300516 Apr 30 '13 at 22:54
  • I added a factory class using a map to create the instance for now, but I think I could solve this dynamically if I can figure out how to pass typeof the string value or get the typeof a class from the string name of the class – user2300516 May 02 '13 at 01:58
  • @user2300516: You just need to get the correct assembly. You can use `typeof(SomeClass).Assembly` to get the assembly containing `SomeClass`. What don't you understand? – SLaks May 02 '13 at 14:21