Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending enums in c#

Tags:

in java im used to extending enum values or overriding methods like this :

    enum SomeEnum     {          option1("sv")         {             public String toString()             {                 return "Some value";             }              },          option2;          private String PassedValue;          public SomeEnum(String somevalue)         {             this.PassedValue = somevalue;         }          public SomeEnum()         {                 this.PassedValue = "Default Value";         }          public String getPassedValue()         {             return this.PassedValue;         }      } 

is there a way to do something similar in c# or are enums more limited in c#

like image 244
Chris McGrath Avatar asked Mar 24 '11 19:03

Chris McGrath


People also ask

Can we extend enum in C?

If you want to extend an existing Dynamics 365 on-premises enum, it is possible to mark a table field in C/SIDE as extensible.

Can you extend enums?

No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.

Can an enum be extended in C++?

No, there is not. enum are really the poor thing in C++, and that's unfortunate of course. Even the class enum introduced in C++0x does not address this extensibility issue (though they do some things for type safety at least).

Can enum extend another enum C#?

Enums cannot inherit from other enums.


1 Answers

I wish enums were more powerful in .Net. And I love .Net! You can use attributes to accomplish the same thing. Write the code below once and use it everywhere. This will be a long answer but I think it's a pretty good solution so have patience!

Usage

SomeEnum e = SomeEnum.ValueTwo; string description = e.GetDescription(); 

The Enum

Use attributes to describe the enum and it's values.

[DescriptiveEnumEnforcement(DescriptiveEnumEnforcement.EnforcementTypeEnum.ThrowException)] public enum SomeEnum {     [Description("Value One")]     ValueOne,      [Description("Value Two")]     ValueTwo,      [Description("Value 3")]     ValueThree } 

DescriptionAttribute

/// <summary>Indicates that an enum value has a description.</summary> [AttributeUsage(AttributeTargets.Field)] public class DescriptionAttribute : System.Attribute {     /// <summary>The description for the enum value.</summary>     public string Description { get; set; }      /// <summary>Constructs a new DescriptionAttribute.</summary>     public DescriptionAttribute() { }      /// <summary>Constructs a new DescriptionAttribute.</summary>     /// <param name="description">The initial value of the Description property.</param>     public DescriptionAttribute(string description)     {         this.Description = description;     }      /// <summary>Returns the Description property.</summary>     /// <returns>The Description property.</returns>     public override string ToString()     {         return this.Description;     } } 

DescriptiveEnumEnforcementAttribute

An attribute to ensure your enum's are properly configured.

/// <summary>Indicates whether or not an enum must have a NameAttribute and a DescriptionAttribute.</summary> [AttributeUsage(AttributeTargets.Enum)] public class DescriptiveEnumEnforcementAttribute : System.Attribute {     /// <summary>Defines the different types of enforcement for DescriptiveEnums.</summary>     public enum EnforcementTypeEnum     {         /// <summary>Indicates that the enum must have a NameAttribute and a DescriptionAttribute.</summary>         ThrowException,          /// <summary>Indicates that the enum does not have a NameAttribute and a DescriptionAttribute, the value will be used instead.</summary>         DefaultToValue     }      /// <summary>The enforcement type for this DescriptiveEnumEnforcementAttribute.</summary>     public EnforcementTypeEnum EnforcementType { get; set; }      /// <summary>Constructs a new DescriptiveEnumEnforcementAttribute.</summary>     public DescriptiveEnumEnforcementAttribute()     {         this.EnforcementType = EnforcementTypeEnum.DefaultToValue;     }      /// <summary>Constructs a new DescriptiveEnumEnforcementAttribute.</summary>     /// <param name="enforcementType">The initial value of the EnforcementType property.</param>     public DescriptiveEnumEnforcementAttribute(EnforcementTypeEnum enforcementType)     {         this.EnforcementType = enforcementType;     } } 

Getting the Description

/// <summary>Provides functionality to enhance enumerations.</summary> public static partial class EnumUtil {     /// <summary>Returns the description of the specified enum.</summary>     /// <param name="value">The value of the enum for which to return the description.</param>     /// <returns>A description of the enum, or the enum name if no description exists.</returns>     public static string GetDescription(this Enum value)     {         return GetEnumDescription(value);     }      /// <summary>Returns the description of the specified enum.</summary>     /// <param name="value">The value of the enum for which to return the description.</param>     /// <returns>A description of the enum, or the enum name if no description exists.</returns>     public static string GetDescription<T>(object value)     {         return GetEnumDescription(value);     }      /// <summary>Returns the description of the specified enum.</summary>     /// <param name="value">The value of the enum for which to return the description.</param>     /// <returns>A description of the enum, or the enum name if no description exists.</returns>     public static string GetEnumDescription(object value)     {         if (value == null)         return null;          Type type = value.GetType();          //Make sure the object is an enum.         if (!type.IsEnum)             throw new ApplicationException("Value parameter must be an enum.");          FieldInfo fieldInfo = type.GetField(value.ToString());         object[] descriptionAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);          //If no DescriptionAttribute exists for this enum value, check the DescriptiveEnumEnforcementAttribute and decide how to proceed.         if (descriptionAttributes == null || descriptionAttributes.Length == 0)         {             object[] enforcementAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptiveEnumEnforcementAttribute), false);              //If a DescriptiveEnumEnforcementAttribute exists, either throw an exception or return the name of the enum instead.             if (enforcementAttributes != null && enforcementAttributes.Length == 1)             {                 DescriptiveEnumEnforcementAttribute enforcementAttribute = (DescriptiveEnumEnforcementAttribute)enforcementAttributes[0];                  if (enforcementAttribute.EnforcementType == DescriptiveEnumEnforcementAttribute.EnforcementTypeEnum.ThrowException)                     throw new ApplicationException("No Description attributes exist in enforced enum of type '" + type.Name + "', value '" + value.ToString() + "'.");                  return GetEnumName(value);             }             else //Just return the name of the enum.                 return GetEnumName(value);         }         else if (descriptionAttributes.Length > 1)             throw new ApplicationException("Too many Description attributes exist in enum of type '" + type.Name + "', value '" + value.ToString() + "'.");          //Return the value of the DescriptionAttribute.         return descriptionAttributes[0].ToString();     } } 
like image 99
Josh M. Avatar answered Nov 03 '22 09:11

Josh M.