Home > Software engineering >  Casting an int to specific Enum at runtime from derived class
Casting an int to specific Enum at runtime from derived class

Time:06-14

I have a group of classes that all are derived from the same base class, and all define unique Enums of the same name.

public class DerivedClassOne : BaseClass
{
    public enum Flags
    {
        None = 0,
        cheese = 1,
        ham = 2,
        egg = 4
    }
}

public class DerivedClassTwo : BaseClass
{
    public enum Flags
    {
        None = 0,
        bacon = 1,
        lettuce = 2,
        tomato = 4
    }
}

How would I, with only an instance of BaseClass, be able to cast an int (n) to this enum and store it at runtime? ie:

object flag = (BaseClassInstance.Enums.Flags)n;

CodePudding user response:

If you don't mind enter image description here

  • Related