I have two Enumerators and a method that takes an enumerator. They are called ABC and DEF and the method is called TestMethod(Enum myEnum). Code is below:
public enum ABC
{
Zero,
One,
Two
};
public enum DEF
{
Zero,
One,
Two
};
public void TestEnum(Enum myEnum)
{
...
}
The function TestEnum takes any enumerator. How can I test which one of the two does the passed in parameter belong to? I could blindly start testing it out with try / catch casting but sheesh that’s ugly. Any cleaner ways of doing this? Thank you in advance for any help.
You can just call
GetType:It’s not clear what you want to do with it after that, mind you.
Alternatively:
EDIT: If you’re able to change the method signature and if your callers will know the type, then as per Jeppe’s comment, you could use:
You can’t constrain
Tto be an enum type with normal C#… although there are hacky ways of writing code with such constraints applied via post-processing.