Reputation: 1362
I am having trouble with this, as I have difficulty properly formulating it too. Making it harder to google it. I will try to explain as clearly as possible. I've simplified the code to make it clearer what my question is
I have an abstract class that has methods and properties that are used by all clases that have this as base class:
public abstract class TheBaseClass{
//some properties here
public enum MyEnum{} // this one every class has. It is pretty much empty here. Not sure if this is best practice.
//some methods here
}
Then there are a number of classes based on this:
public SpecializedClass : TheBaseClass{
//some more properties here
public new enum MyEnum{} //every single class has a different enum
//some more methods here
}
Now, somewhere else in the code, I have a method
public void MyMethod(TheBaseClass baseclassobject){
//do stuff
var usedforsomething = ((TheBaseClass.MyEnum)i).ToString() //i being an int. This is used to name something in a plot.
//do more stuff
}
The reason for using TheBaseClass as a parameter for the method, is that before I had very long code that did what mymethod did for each class derived from TheBaseClass. It is not good to have duplicate code, so I made this method instead and wanted to call it with the parameter SpecializedClass (and many other classes like that also). The problem is that when calling TheBaseClass.MyEnum I naturally get the enum for the BaseClass, not the one from SpecializedClass. I have been experimenting with how to get the right enum in the method regardless of what baseclassobject I give it, but can't seem to find the solution.
How can I get the enum of whatever class baseclassobject is? I have tried some different things, but don't seem to work. The problem I think is that the enum is not a property or method that I can call from the object, but rather need to call ClassName.MyEnum, which I don't have the className in the method.
A solution could be to create a method for each class type, with that specific class type as parameter, but that seems like a lot of duplicate code.
For example if I have 50 different derived classes like SpecializedClass
Upvotes: 6
Views: 251
Reputation: 46947
I think reflection would be you only option here.
var usedforsomething =
baseclassobject
.GetType()
.GetNestedType(nameof(TheBaseClass.MyEnum))
.GetEnumName(i);
But perhaps a better solution would be to add an abstract function GetName
in your base class that your child classes must override.
public abstract class TheBaseClass
{
public enum MyEnum {a,b }
public abstract string GetName(int value);
}
public class SpecializedClass : TheBaseClass
{
public new enum MyEnum {c,d }
public override string GetName(int value)
{
return ((MyEnum)value).ToString();
}
}
You could than do:
var usedforsomething = baseclassobject.GetName(i);
You can than avoid reflection and also the dependence on the child classes declaring the enum with the specific name MyEnum
.
Upvotes: 5
Reputation: 1720
May I understood you incorrectly, but did you try to do little reflection stuff in MyMethod
to take necessary enum type:
public static void MyMethod(TheBaseClass baseclassobject)
{
Type enumType = baseclassobject.GetType().GetNestedType("MyEnum");
var usedforsomething = Enum.GetName(enumType, 1);
Console.WriteLine(usedforsomething);
}
Upvotes: -1