我发现了一个非常基本的代码,如下所述,但我无法让它在我的 c# windows 窗体解决方案中工作。我收到错误:
'System.Enum.TryParse(string, out string)' 的最佳重载方法匹配有一些无效参数
-
参数 1:无法从“System.Type”转换为“string”
public enum PetType
{
None,
Cat = 1,
Dog = 2
}
string value = "Dog";
PetType pet = (PetType)Enum.TryParse(typeof(PetType), value);
if (pet == PetType.Dog)
{
...
}
我不明白问题出在哪里。错误全在Enum.TryParse
线。任何想法?
Thanks.
从文档中可以看出,Enum.TryParse<TEnum> http://msdn.microsoft.com/en-us/library/dd783499.aspx是返回布尔属性的通用方法。您使用它的方式不正确。它使用一个out
存储结果的参数:
string value = "Dog";
PetType pet;
if (Enum.TryParse<PetType>(value, out pet))
{
if (pet == PetType.Dog)
{
...
}
}
else
{
// Show an error message to the user telling him that the value string
// couldn't be parsed back to the PetType enum
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)