jeudi 22 octobre 2020

Retrieving CustomAttribute of a new enum which is created from another 2 enums in c#

I have 2 enums with Customattributes on enumvalues like below

public enum enum1
{
     [Identifier("Identifier11")]
     enum1value1,
     [Identifier("Identifier12")]
     enum1value2
}

public enum enum2
{
     [Identifier("Identifier21")]
     enum2value1,
     [Identifier("Identifier22")]
     enum2value2
}

I have created an enum enum3, combining both in another file.

public enum enum3
{
     enum3value1 = enum1.enum1value1,
     enum3value2 = enum1.enum1value2,
     enum3value3 = enum2.enum2value1,
     enum3value4 = enum2.enum2value2
}

I wanted to retrieve the custom 'IdentifierAttribute' of each of the enums.

I used the following code:

        var enumType = enumValue.GetType();
        var enumItem = Enum.GetName(enumType, enumValue);
        var customAttribute = enumType.GetField(enumItem).GetCustomAttributes(false).OfType<IdentifierAttribute>().SingleOrDefault();

But it is returning null. As per my understanding, when enum3 is assigned values of enum1 and enum2, the attribute tags are lost. How do i retrieve the attributes??





Aucun commentaire:

Enregistrer un commentaire