0

i have written a generic method to convert int to Enum String, getting an error. Please Help Thanx.

    public static string GetStringEquiValentOFEnumFromString<T>(int enumVal)
        where T : struct
    {
        if(Enum.IsDefined(typeof(T),enumVal))
        {
            return ((T)enumVal).ToString();  ///Error: Cannot convert type 'int' to 'T'
        }          
        return null;  
    }
Rusi Nova
  • 2,617
  • 6
  • 32
  • 48

4 Answers4

2

I think you can use Enum.ToObject:

public static string GetStringEquiValentOFEnumFromString<T>(int enumVal)
     where T : struct
    {
        if (Enum.IsDefined(typeof(T), enumVal))
        {
            return Enum.ToObject(typeof (T), enumVal).ToString();
        }
    }

http://msdn.microsoft.com/en-us/library/system.enum.toobject.aspx

DMac the Destroyer
  • 5,240
  • 6
  • 36
  • 56
0

public static class StringEnum {

    public static string GetStringValue(Enum value)
    {
        string output = null;
        Type type = value.GetType();

        FieldInfo fi = type.GetField(value.ToString());
        StringValue[] attr = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[];

        if (attr.Length > 0)
        {
            output = attr[0].Value;
        }
        return output;
    }
}

StringEnum is a class which have (GetStringValue) method to get the string value.

public enum CampaignRequestType { [StringValue("None")] None = 0, [StringValue("Pharmacy Cards")] Pharmacy_Cards = 1,[StringValue("Prospect Campaign")] Prospect_Campaign = 2,[StringValue("Tradeshow/Advertising")] Tradeshow_Advertising = 3 }

its a enum...

string item = StringEnum.GetStringValue((Enumeration.CampaignRequestType)updateRequestStatus.RequestType_Code);

here (Enumeration.CampaignRequestType) is my enumeration and updateRequestStatus.RequestType_Code is data base field int type

i cast int value to enumeration type

Rohit
  • 1
0

You're seeing this error because generic type arguments cannot be constrained to the enum type in C#. Since your constraint only says it has to be a value type the compiler cannot guarantee that enumVal will be convertible to T and gives a compile time error.

To get around this you can explicitly define the enum type in the function:

public static string GetStringFromValue(this MyEnum enum, int val) {
   if (Enum.IsDefined(typeof(MyEnum), val)) {
      return ((MyEnum) val).ToString();
   }
   return null;
}

Or you can use something like Jon Skeet's UnconstrainedMelody library which rewrites your code at the bytecode level to enforce the enum constraint (callable from another assembly).

Ron Warholic
  • 9,994
  • 31
  • 47
  • there's another wrinkle, which is that the enum might have a smaller type than `int` as its underlying type. – phoog Feb 16 '12 at 21:58
0

This ought to work:

public static string GetStringEquiValentOFEnumFromString<T>(int enumVal) 
    where T : struct 
{ 
    if(Enum.IsDefined(typeof(T),enumVal)) 
    { 
        return ((T)(object)enumVal).ToString();
    }           
    return null;   
} 
phoog
  • 42,068
  • 6
  • 79
  • 117