-4

Because I haven't entered the namespace + the name of the class (I only entered the class name) i received an Exception error.

I have a lot of classes written and I receive a String. I want directly to declare and initiate the class that the value of the String is. I don't want to browse every single class written to see which is. Here is an example:

class Car
{
      public void startCar()
      {
            Console.WriteLine("Car started");
      }
}

class Main
{
      private void treeView1_Click(object sender, EventArgs e)
      {
         String s="Car"
        // I said Car obj because that's the value of the string
         Car obj = new Car();

         // or like this
         value.string obj = new value.string();
         obj.startCar();


      }



}

2 Answers2

1

You have to use reflection, but you need a fully qualified class name, so if you have only the class name, you have to decide a default namespace:

Type objType = Type.GetType("TheNamespace." + className); 
object obj = Activator.CreateInstance(objType);
MethodInfo myMethod = objType.GetMethod(methodName);
myMethod.Invoke(obj,   // The object on which to invoke the method
                null); // Argument list for the invoked method

If all the classes you have to instantiate expose the same method that then you have to call, you can define a common Interface for all those classes:

interface MyInterface
{
    void showData();
}

class MyClass : MyInterface
{

    public void showData()
    {
        Console.WriteLine("MyClass");
    }
}

class MyClass2 : MyInterface
{
    public void showData()
    {
        Console.WriteLine("MyClass2");
    }
}

Then cast the instance of the object to MyInterface and invoke the method:

// Using MyClass
Type objType = Type.GetType("VsLogAnalyzer." + "MyClass");
MyInterface myObj = Activator.CreateInstance(objType) as MyInterface;
myObj.showData();
// Using MyClass2
objType = Type.GetType("VsLogAnalyzer." + "MyClass2");
myObj = Activator.CreateInstance(objType) as MyInterface;
myObj.showData();
Matteo Umili
  • 3,412
  • 1
  • 19
  • 31
  • @ClaudiuLazăr do all types that you have to instantiate expose `startCar()` method? Or do you have also to call it from a string? – Matteo Umili Jul 24 '15 at 07:30
  • i have only one method in every single class and it's named showData() with no parameters. I keep getting null pointer exception when i try to invoke the function – Claudiu Lazăr Jul 24 '15 at 07:37
  • @ClaudiuLazăr How do you invoke it? What does the exception's stack trace says? – Matteo Umili Jul 24 '15 at 07:46
0
  // I had the problem that the program didn't find the specific class
  // This is the code that I used and it helped me resolve the exception by entering the namespace also




     // the s is the string

       Type objType = Type.GetType("Namespace." + s);
        object obj = Activator.CreateInstance(objType);


        MethodInfo Method = objType.GetMethod("showData");
        object Value = Method.Invoke(obj,null);
        richTextBox1.Text = Value.ToString();

THX a lot @codroipo