Converting/Casting 我在 C# 中对对象的枚举
Converting/Casting my Enum to Object in C#
在我的 GetData(MethodInfo testMethod)
中,我正在尝试 return 我的 Role
属性 类型 Role
是一个枚举。
public class RoleAttribute : DataAttribute
{
public Role Role { get; set; }
public RoleAttribute(Role role)
{
Role = role;
AuthRespository.Login(role);
}
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
return Role.ToObject<List<object[]>>();
}
}
但我收到一条错误消息:
Error CS0308 The non-generic method 'Enum.ToObject(Type, byte)' cannot be used with type arguments
GetData(MethodInfo testMethod)
是 DataAttribute
class 的抽象成员。
让属性执行功能(即 AuthRespository.Login(role);
),
只需要 GetData
return 想要的数据
public override IEnumerable<object[]> GetData(MethodInfo testMethod) {
yield return new object[] { Role };
}
或
public override IEnumerable<object[]> GetData(MethodInfo testMethod) {
return new[] { new object[] { Role } };
}
在我的 GetData(MethodInfo testMethod)
中,我正在尝试 return 我的 Role
属性 类型 Role
是一个枚举。
public class RoleAttribute : DataAttribute
{
public Role Role { get; set; }
public RoleAttribute(Role role)
{
Role = role;
AuthRespository.Login(role);
}
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
return Role.ToObject<List<object[]>>();
}
}
但我收到一条错误消息:
Error CS0308 The non-generic method 'Enum.ToObject(Type, byte)' cannot be used with type arguments
GetData(MethodInfo testMethod)
是 DataAttribute
class 的抽象成员。
让属性执行功能(即 AuthRespository.Login(role);
),
只需要 GetData
return 想要的数据
public override IEnumerable<object[]> GetData(MethodInfo testMethod) {
yield return new object[] { Role };
}
或
public override IEnumerable<object[]> GetData(MethodInfo testMethod) {
return new[] { new object[] { Role } };
}