c# - 帮助枚举和 Enum.Parse

标签 c# .net string class enums

我是枚举新手。

我有这个枚举:

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/

相关文章:

c# - 您能否以编程方式与基于组件的服务 (TrustedInstaller) 交互?

c# - 如何将 HTML 代码放入 .resx 资源文件?

.net - 如何使用 ClickOnce 安装 COM

c++ - 检查一个字符串 C++ 中的多个信号

Python Split 特定案例处理

Python有没有办法计算字符串插入字符串的数量?

c# - OwinStartup 和在 asp.net mvc 的 signalr 中启动

.net - 编程式远程应用程序安装

c# - 如何使用activeMQ NMS和C#实现同步请求-响应应用程序?

c# - 验证 Ajax 加载的表单 - 返回验证摘要