问题描述
|
if (user.Access == (UserType)Enum.Parse(typeof(UserType),str,true))
我有一个通道机器人,其命令如下:
.add username usertype
现在,我必须检查用户名是否已经具有该写访问权,但是当用户类型不完全像枚举那样写时,它总是失败。
例如,写为headadmin的HeadAdmin导致以下错误:
System.NullReferenceException: Object reference not set to an instance of an object.
另外,如果我键入一个不存在的枚举,它将告诉我找不到值X。
如何转换用户类型,以便如果匹配则返回true,如果不匹配或不存在,则返回false?
解决方法
给定您的示例代码和所述错误,我的第一个假设是
user
对象为null
。