I have a base class with a number of child classes. Each class have a GetType property that will return a specified enum value. Is it possible to get the class name when I supply the enum value?
Example:
public enum EnumClassNames
{
Class1 = 1,
Class2 = 2,
}
class MySubClass : ParentClass
{
public override EnumType MyType()
{
return EnumType.Class1;
}
}
void main()
{
var className = xxxx(EnumType.Class1); //I would like to get the value MySubClass back here.
}
Edit: A little background. I have a Message parent class that will process messages in a queue. Each 'kind-of' message subclass will override a ProcessMessage function.
I also have Queue class that will iterate through the Queue and process each message. So the "correct" option will probably be to explicitly instantiate an object of each child class in the Queue class and call ChildClass.ProcessMessage, but to minimise maintenance with new MessageTypes added, I want to "read" the name of the child class and instantiate an object from there. This will then prevent extra if- and switch statements when new Message types are added.
I wouldn't create a method for that, as you need an instance to call that method. You could however create a static property which returns the type. Maybe an attribute would even be better.
I created a fiddle as an example: https://dotnetfiddle.net/IweLy7
It relies on scanning the assembly for relevant types. As stated by the other answer you may have to check additional assemblies for your usecase.