【发布时间】:2012-05-21 13:04:17
【问题描述】:
我正在尝试构建从用户字符串获取的通用函数,并尝试将其解析为 Enum 值,如下所示:
private Enum getEnumStringEnumType(Type i_EnumType)
{
string userInputString = string.Empty;
Enum resultInputType;
bool enumParseResult = false;
while (!enumParseResult)
{
userInputString = System.Console.ReadLine();
enumParseResult = Enum.TryParse(userInputString, true, out resultInputType);
}
}
但我明白了:
The type 'System.Enum' must be a non-nullable value type in order to use it as parameter 'TEnum' in the generic type or method 'System.Enum.TryParse<TEnum>(string, bool, out TEnum) .
错误意味着我需要为 resultInputType 声明一个特定的枚举? 我怎样才能解决这个问题 ? 谢谢。
【问题讨论】:
-
当你说“泛型函数”时——你的方法不是泛型的。您是否需要能够将类型指定为
Type值而不是使其成为真正的泛型方法?