I am trying to make a helper function that return an enum name of any type of enum- working from: https://msdn.microsoft.com/en-us/library/system.enum.getname%28v=vs.110%29.aspx
I have run into this error:
Error 1 The type or namespace name 'enumType' could not be found (are you missing a using directive or an assembly reference?)
From this func:
public static string EnumNameToString<T>( T enumType , T enumActual)
{
return Enum.GetName( typeof( enumType ), enumActual );
}
Erroring on this line:
return Enum.GetName( typeof( enumType ), enumActual );
I have had a quick search around but seem to only find that people renaming projects etc are getting this error rather than this seems to me using generics.
Thanks in advance :)
EDIT :
enum eEnumType
{
eEnum0,
eEnum1,
eEnum2
}
enum eEnumType2
{
e_Enum0,
e_Enum1,
e_Enum2
}
eEnumType anEnum;
eEnumType2 anEnum2;
string exampleStr = EnumNameToString<string>( eEnumType, eEnum0 );
string exampleStr2 = EnumNameToString<string>( eEnumType2, e_Enum2 );
Expect exampleStr to be "eEnum0". Expect exampleStr2 to be "e_Enum2".
(New to generics so this could still be quite off, but hopefully gives insight to what I'm attempting to achieve).
Thanks again.
Thanks Machinarius ( EDIT : and Alex K. ) I made it work from your answer... Was getting confused with the generics.
Answer:
public static string EnumNameToString<T>(T enumActual)
{
return Enum.GetName( typeof( T ), enumActual );
}
Used like:
string str = Helper.EnumNameToString<eEnumType>( eEnumType.eEnum0 );
Works perfectly. Thanks- a grateful nub!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.