Is it possible to get the enum value of a base class, from a derived class, without using reflection ?
The code below using Reflection, which seems a bit of overkill to get a base class field value from an object instance.
using System;
namespace Seans
{
public class BaseClass
{
public enum eEnum{a, b, c}
}
class Program
{
static void Main(string[] args)
{
DerivedClassA newObject = new DerivedClassA();
TraverseTheObjectHierachyForTheTaskListEnum(newObject.GetType());
Console.ReadLine();
}
public static Type GetTaskListType(Type type)
{
// Handle the cases where our enums are defined in the base class
return TraverseTheObjectHierachyForTheTaskListEnum(type);
}
private static Type TraverseTheObjectHierachyForTheTaskListEnum(Type type)
{
foreach (Type nestedType in type.GetNestedTypes())
{
if (nestedType.IsEnum)
{
// Enum Name, now you can get the enum values...
Console.WriteLine(nestedType.FullName);
return nestedType;
}
}
if (type.BaseType != null)
{
return TraverseTheObjectHierachyForTheTaskListEnum(type.BaseType);
}
return null;
}
}
}