【发布时间】:2013-12-10 09:03:30
【问题描述】:
您好,我不知道如何调用下一个函数,请您帮帮我。
函数检查值是否定义为枚举值。如果不是,则抛出异常。 警告:[Flag] 类型的枚举失败
public static T FailIfEnumIsNotDefined<T>(this T enumValue, string message = null)
where T:struct
{
var enumType = typeof (T);
if (!enumType.IsEnum)
{
throw new ArgumentOutOfRangeException(string.Format("Type {0} is not an Enum, therefore it cannot be checked if it is Defined not have defined.", enumType.FullName));
}
else if (!Enum.IsDefined(enumType, enumValue))
{
throw new ArgumentOutOfRangeException(string.Format("{1} Value {0} is not does not have defined value in Enum of type {0}. It should not be...", enumType.FullName, message ?? ""));
}
return enumValue;
}
我尝试过类似的方法,但出现错误。
var valueFormatted = tobeTested.FailIfNullOrEmptyEnumerable<string>();
【问题讨论】:
-
您必须向我们提供有关该功能应该做什么的更多信息
-
string是class而不是struct并且您调用该方法的方式不正确。 -
对提供的枚举类型进行扩展似乎没有用。您需要对字符串或对象进行扩展。如果您需要,请检查我的答案