.Net Enum.IsDefined:我错过了什么? [英] .Net Enum.IsDefined: What am I missing?

查看:121
本文介绍了.Net Enum.IsDefined:我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net 4.5 Enum.IsDefined 定义包含以下两个对我来说非常含糊的陈述。



定义

The .Net 4.5 Enum.IsDefined definition includes the following two statements which appear highly ambiguous to me.

Definition

public static bool IsDefined(
    Type enumType,
    Object value
)



true 如果中的常量> enumType 的值等于 value ;否则, false



例外

< u> ArgumentException



值的类型是枚举,但它不是enumType类型的枚举。

- 或者 -

值的类型不是enumType的基础类型。





我不记得它。获得 false 结果的唯一方法是提供一个数字,任何提供其他任何东西的尝试都将是 true 或将抛出异常。



看起来像是一种过于复杂的方式告诉用户他们的确切错误是什么错误,结果是很明显。



具有讽刺意味的是,通过枚举的反馈似乎更容易理解。

我是否理解这一点?



谢谢,

M


true if a constant in enumType has a value equal to value; otherwise, false.

Exceptions
ArgumentException

The type of value is an enumeration, but it is not an enumeration of type enumType.
-or-
The type of value is not an underlying type of enumType.


I don'tget it. The only way to get a result of false is by providing a number, any attempt to provide anything else will provide will either be true or will throw an exception.

Seems like an over-complicated way of telling the userof the eumeration exactly what error they've made and by consequence less than obvious.

Ironically, feedback via an enumeration would have seemed more comprehensible.
Have I understood this correctly?

Thanks,
M

推荐答案

引用:

我不记得了。得到假结果的唯一方法是提供一个数字......

I don'tget it. The only way to get a result of false is by providing a number...



如果你的意思是提供一个数字,如下所示:


If you mean "providing a number" as in:

enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
...
var day = (Days)42;
...
if (Enum.IsDefined(typeof(Days), day)) ...



然后我认为你是对的:这是得到假的唯一方法(或者,就此而言,任何其他让你达到这一点的方式)。



就个人而言,我尽量避免 Enum。 IsDefined()并且只与 Debug.Assert()和后一类中的其他方法一起使用。对于程序的发布版本,我更喜欢为每个枚举类定义扩展方法(在适当的时候)以检查有效值,有时还要检查其他不变规则。大多数情况下,使用开关语句。它已被证明是一种很好的技术。



也许 Enum.IsDefined()可证明自己最有价值其他情况。例如,类似地,应用于原始输入数据的一般外化验证规则。我的意思是,当你想要在程序之外定义 的业务规则时,而不是硬编码到它中。不过只是想一想。



我希望自己清楚;这是一个抽象的想法。

好​​问题。


Then I think you're right: that's the only way of getting a result of false (or, for that matter, any other way that gets you to that point).

Personally, I try to avoid Enum.IsDefined() and only use it with Debug.Assert() and other methods in this latter class. For the release version of the program, I prefer to define extension methods for each enum class (when appropriate) to check for valid values and, sometimes, other invariant rules. Most of the times, by using a switch statement. It has proven to be a good technique.

Maybe Enum.IsDefined() can prove itself most valuable in other circumstances. Like, for example, general "externalized" validation rules applied to raw input data. I mean, when you want the business rules defined outside the program, and not hardcoded into it. Just a thought, though.

I hope I made myself clear; it's kind of an abstract idea.
Good question.


这篇关于.Net Enum.IsDefined:我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆