1

There exists an enum in an assembly:

public enum TheEnumeration
{
    TheFirstValue = 1,
    TheSecondValue = 2
}

In another Assembly I would like to extend this enumeration with some Attributes (I know this is not valid code, just to show the idea):

public enum MyExtendedEnumeration : TheEnumeration
{
    [MyAttribute("The First Value")]
    TheFirstValue,

    [MyAttribute("The 2nd Value")]
    TheSecondValue
}

Is there a way to achieve this goal in a proper way?

Roland Bär
  • 1,720
  • 3
  • 22
  • 33

2 Answers2

1

You can't Extend enums, you can't inherit from them. You may just have to create a new Enum that repeats the values like a pass through and then decorate yours.

public enum MyExtendedEnumeration
{
    [MyAttribute("The First Value")]
    TheFirstValue = TheEnumeration.TheFirstValue,

    [MyAttribute("The 2nd Value")]
    TheSecondValue = TheEnumeration.TheFirstValue
}

See: Extending enums in c#

Community
  • 1
  • 1
reckface
  • 5,678
  • 4
  • 36
  • 62
0

Enums cant inherit from another Enum. They are based on the System.Enum You can put Attributes on the members.

Creating a class/Type that behaves somewhat like an Enum, may be a useful in scenarios like this. Assumes you can "alter" the orginal enum.

///
/// Sample of a STRING or non int based enum concept.
/// 

public sealed class FilterOp {
    private static readonly Dictionary<string, FilterOp> EnumDictionary = new Dictionary<string, FilterOp>();
    private readonly string _name;
    private readonly string _value;

    public const string Eq = "Eq";
    public const string Ne = "Ne";
    public const string Gt = "Gt";
    public const string Ge = "Ge";
    public const string Lt = "Lt"; 
    public const string Le = "Le";
    public const string And = "And";
    public const string Or = "Or";
    public const string Not = "Not";

    public static readonly FilterOp OpEq = new FilterOp(Eq);
    public static readonly FilterOp OpNe = new FilterOp(Ne);
    public static readonly FilterOp OpGt = new FilterOp(Gt);
    public static readonly FilterOp OpGe = new FilterOp(Ge);
    public static readonly FilterOp OpLt = new FilterOp(Lt);
    public static readonly FilterOp OpLe = new FilterOp(Le);
    public static readonly FilterOp OpAnd = new FilterOp(And);
    public static readonly FilterOp OpOr = new FilterOp(Or);
    public static readonly FilterOp OpNot = new FilterOp(Not);


    private FilterOp(string name) {
        // extend to cater for Name / value pair, where name and value are different
        this._name = name;
        this._value = name;
        EnumDictionary[this._value] = this;
    }

    public override string ToString() {
        return this._name;
    }

    public string Name {
        get { return _name; }
    }

    public string Value {
        get { return _value; }
    }

    public static explicit operator FilterOp(string str) {
        FilterOp result;
        if (EnumDictionary.TryGetValue(str, out result)) {
            return result;
        }
        return null;
    }
}
phil soady
  • 11,043
  • 5
  • 50
  • 95