错误 1 找不到类型或命名空间名称 *(是否缺少 using 指令或程序集引用?)
Error 1 The type or namespace name * could not be found (are you missing a using directive or an assembly reference?)
我正在尝试制作一个辅助函数,该函数 return 任何类型枚举的枚举名称 - 工作来自:https://msdn.microsoft.com/en-us/library/system.enum.getname%28v=vs.110%29.aspx
我 运行 遇到这个错误:
Error 1 The type or namespace name 'enumType' could not be found (are
you missing a using directive or an assembly reference?)
来自这个函数:
public static string EnumNameToString<T>( T enumType , T enumActual)
{
return Enum.GetName( typeof( enumType ), enumActual );
}
这一行出错:
return Enum.GetName( typeof( enumType ), enumActual );
我快速搜索了一下,但似乎只发现重命名项目等的人遇到了这个错误,而不是在我看来使用泛型。
提前致谢:)
编辑:
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 );
预计 exampleStr 为 "eEnum0"。
期望 exampleStr2 为 "e_Enum2".
(泛型的新手,所以这可能仍然很离谱,但希望能深入了解我正在尝试实现的目标)。
再次感谢。
谢谢Machinarius (EDIT: and Alex K.)我根据你的回答让它工作了......对泛型感到困惑。
答案:
public static string EnumNameToString<T>(T enumActual)
{
return Enum.GetName( typeof( T ), enumActual );
}
像这样使用:
string str = Helper.EnumNameToString<eEnumType>( eEnumType.eEnum0 );
完美运行。谢谢 - 一个感激的小块!
我正在尝试制作一个辅助函数,该函数 return 任何类型枚举的枚举名称 - 工作来自:https://msdn.microsoft.com/en-us/library/system.enum.getname%28v=vs.110%29.aspx
我 运行 遇到这个错误:
Error 1 The type or namespace name 'enumType' could not be found (are you missing a using directive or an assembly reference?)
来自这个函数:
public static string EnumNameToString<T>( T enumType , T enumActual)
{
return Enum.GetName( typeof( enumType ), enumActual );
}
这一行出错:
return Enum.GetName( typeof( enumType ), enumActual );
我快速搜索了一下,但似乎只发现重命名项目等的人遇到了这个错误,而不是在我看来使用泛型。
提前致谢:)
编辑:
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 );
预计 exampleStr 为 "eEnum0"。 期望 exampleStr2 为 "e_Enum2".
(泛型的新手,所以这可能仍然很离谱,但希望能深入了解我正在尝试实现的目标)。
再次感谢。
谢谢Machinarius (EDIT: and Alex K.)我根据你的回答让它工作了......对泛型感到困惑。
答案:
public static string EnumNameToString<T>(T enumActual)
{
return Enum.GetName( typeof( T ), enumActual );
}
像这样使用:
string str = Helper.EnumNameToString<eEnumType>( eEnumType.eEnum0 );
完美运行。谢谢 - 一个感激的小块!