How to submit just a class name as a parameter of a method?
-
Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?
-
Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?
Create a public enum of say ClassTypes and pass it to the method. Apply switch to ClassTypes and generate classes as you may require. e.g public enum ClassType { ClassA, ClassB } classCInstance.CreateClass(ClassType objClass) { switch(objClass) { case ClassType.ClassA: new ClassA(); break; case ClassType.ClassB: new ClassB(); break; } } P.S: This is just an example for your understanding. Hope it helps Ahsan Ullah Senior Software Engineer MCTS 2.0
-
Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?
C# reflection allows you to create instances of a type based on the classes string representation (i.e., the name of the class). Specifically, the article "Dynamically Loading and Using Types" should be of particular use to you.
-
C# reflection allows you to create instances of a type based on the classes string representation (i.e., the name of the class). Specifically, the article "Dynamically Loading and Using Types" should be of particular use to you.
-
Create a public enum of say ClassTypes and pass it to the method. Apply switch to ClassTypes and generate classes as you may require. e.g public enum ClassType { ClassA, ClassB } classCInstance.CreateClass(ClassType objClass) { switch(objClass) { case ClassType.ClassA: new ClassA(); break; case ClassType.ClassB: new ClassB(); break; } } P.S: This is just an example for your understanding. Hope it helps Ahsan Ullah Senior Software Engineer MCTS 2.0
Thanks, this looks good. The only disadvantage would be that I have to manage the enum and keep it up to date. I expect to have hundrets of different classes that would be listed in the enum. Would it be possible to get all the available classes? Basically I have those classes in a separate folder in visual studio and they are all derived from the same base class which could maybe used for the search.
-
Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?
I think, the Type class in .NET will help you. You can pass the type of the class to your function and create instances using Reflection.
class YourClass
{
......
}.....
public void YourMethod(Type type) { // do your stuffs here.. // you can use Reflection to create the instance. } public void AnotherMethod() { YourMethod(typeof(YourClass)); }
Do more work Make more mistakes Learn more things
-
Thanks, that would be a solution, however it would force me to create a DLL. I would prefer to have those classes in my .exe.
It doesn't need to be in a DLL to create the type. That's just what the example used. I'm pretty sure you can skip the step of loading the assembly altogether if the type is in the current assembly (i.e., if it is in the current EXE).
-
Hello, I want to submit somehow a class name to a method and inside the method i would like to create multiple instances of the submitted class. The reason is that I want to call the constructor of a another class and want to tell which other class instances should be created. How can i do that?
Or pass in a ConstructorInfo instead -- make the calling method do the Reflection.