0

I try to use enumeration with string value for that i use Attribute but it showing error as Extension method must be defined in a non-generic static class

public enum CommonMessagesEnum : int
{
    [StringValue("This Operation is not allowed.")]
    NotAllowed = 1
}

public class StringValueAttribute : Attribute
{        

    public string StringValue { get; protected set; }    


    public StringValueAttribute(string value)
    {
        this.StringValue = value;
    }        

    public static string GetStringValue(this Enum value)
    {
        Type type = value.GetType();
        FieldInfo fieldInfo = type.GetField(value.ToString());            
        StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];        
        return attribs.Length > 0 ? attribs[0].StringValue : null;
    }

}
Dinesh
  • 1,005
  • 5
  • 16
  • 38

1 Answers1

2

You have to move your method to a static class

public static class ExtensionMethods
    {
        public static string GetStringValue(this Enum value)
        {
            Type type = value.GetType();
            FieldInfo fieldInfo = type.GetField(value.ToString());
            StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
            return attribs.Length > 0 ? attribs[0].StringValue : null;
        }
    }
Moatasem bakri
  • 147
  • 1
  • 4
  • 16