简体   繁体   中英

Error 1 The type or namespace name * could not be found (are you missing a using directive or an assembly reference?)

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.

Related Question error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) Why this error ? “The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)” Error 1 The type or namespace name 'FPSTimer' could not be found (are you missing a using directive or an assembly reference?) error CS0246: The type or namespace name 'BannerPosition' could not be found are you missing a using directive or an assembly reference? c# Error 1 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?) Getting error The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) Error: The type or namespace name 'SqlCe' could not be found (are you missing a using directive or an assembly reference?) Error 1 The type or namespace name 'ProductServiceClient' could not be found (are you missing a using directive or an assembly reference?) Error 14 The type or namespace name 'Document' could not be found (are you missing a using directive or an assembly reference?) Error CS0246 The type or namespace name 'CreateRandomAnswersForKey' could not be found (are you missing a using directive or an assembly reference?)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM