我是枚举新手。
我有这个枚举:
public enum Categories
{
Animals,
Animations,
Accessories,
Apearance,
Clothing,
Gadgets,
Land,
Scripts,
Vehicles,
Weapons,
Other
}
然后我有这个变量:private Categories Category;
我正在尝试解析用户输入(字符串),以便 Category
等于正确的枚举。
this.Category = Enum.Parse(Categories ,cat);
我得到这个错误:
'Product.Categories' is a 'type' but is used like a 'variable'
我希望你能理解我想说的。
最佳答案
要获取 Type
对象以与 Enum.Parse()
等方法一起使用,请使用带有类型名称的 typeof
运算符。您还需要执行从 object
(它返回)到您的枚举的转换:
this.Category = (Categories) Enum.Parse(typeof(Categories), cat);
关于c# - 帮助枚举和 Enum.Parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5901335/