测试按位Enum值
|
我以前从未真正使用过按位枚举,我只是想确保测试正确。我对测试值None和All最感兴趣。我们从Web服务接收数据,该服务利用此枚举对数据的某些部分进行分类。鉴于此,我假设“无”或“全”将与任何其他值组合。
给定以下按位枚举定义;
[System.FlagsAttribute()]
public enum TrainingComponentTypes : int
{
None = 0,
AccreditedCourse = 1,
Qualification = 2,
Unit = 4,
SkillSet = 8,
UnitContextualisation = 16,
TrainingPackage = 32,
AccreditedCourseModule = 64,
All = 127,
}
我在该MSDN网站上阅读了有关FlagAttributes的以下引用;
使用None作为标志名称
枚举常量,其值为
零。您不能使用无
枚举常数按位与
用于测试标志的操作,因为
结果始终为零。然而,
您可以执行逻辑,而不是
按位比较
数值和无枚举
常数以确定是否有任何位
在数值中设置。
在这种情况下,逻辑比较是否引用枚举的正常相等性测试?
例如;
TrainingComponentTypes tct = TrainingComponentTypes.None;
if (tct == TrainingComponentTypes.None)
{ ... }
为了进行逐位比较,我正在执行以下操作;
TrainingComponentTypes tct = TrainingComponentTypes.AccreditedCourse | TrainingComponentTypes.Qualification | TrainingComponentTypes.TrainingPackage;
Assert.IsTrue((tct & TrainingComponentTypes.AccreditedCourse) == TrainingComponentTypes.AccreditedCourse, \"Expected AccreditedCourse as part the enum\");
Assert.IsFalse((tct & TrainingComponentTypes.SkillSet) == TrainingComponentTypes.SkillSet, \"Found unexpected SkillSet as part the enum\");
最后,在测试所有对象时,我尝试了逻辑和按位比较,并且它们都返回相同的结果。我应该在这里使用另一个吗?例如;
TrainingComponentTypes tct = TrainingComponentTypes.All;
Assert.IsTrue((tct & TrainingComponentTypes.All) == TrainingComponentTypes.All, \"Expected All as part the enum\");
Assert.IsTrue((tct) == TrainingComponentTypes.All, \"Expected All as part the enum\");
// The follow also pass the assertion for a value of All
Assert.IsTrue((tct & TrainingComponentTypes.Qualification) == TrainingComponentTypes.Qualification, \"Expected Qualification as part the enum\");
Assert.IsTrue((tct & TrainingComponentTypes.TrainingPackage) == TrainingComponentTypes.TrainingPackage, \"Expected TrainingPackage as part the enum\");
因此,总而言之,我想了解以下有关逐位枚举的信息;
我对逻辑的理解
给我的例子比较正确
以上?
是我表演的方式
按位比较正确吗?
什么是处理“全部”的正确方法
值(按位或逻辑)。我不确定是否会收到将All与其他TrainingComponentTypes组合在一起的值。我不明白为什么要这么做,但是那你永远不知道吗?
我是正确的假设
声明基本上不应该是
用于按位枚举(无)
似乎是特例,
需要逻辑比较)?
谢谢,
克里斯
没有找到相关结果
已邀请:
6 个回复
量华
版本将失败。例如:
对于这一部分: 我不确定是否会收到将All与其他TrainingComponentTypes组合在一起的值。 我不确定您是否完全理解枚举的工作原理。
如您所见,“ All”只是所有这些值的按位“ 7”。您无法将任何其他TraningComponentTypes与All结合使用,因为All已包含它们!另外,如果您自己将它们与with7组合在一起,则与直接使用All完全相同(因此,在枚举中定义All时很方便)。 4)您可以使用它来检查无或全部,但不能检查其他值。 值得注意的是,Enum上有一个方便的方法可以为您执行以下检查:Enum.HasFlag。
桑娠贯涤
。例如:
代替:
什么是处理\“ All \”值(按位或逻辑)的正确方法。我不确定是否会收到将All与其他TrainingComponentTypes组合在一起的值。我不明白为什么要这么做,但是那你永远不知道吗? 我认为最好将
中的ѭ12定义为所有部分的按位“或”。但是,您会看到人们同时做这两种事情。 我是否假设基本上不应该将switch语句用于按位枚举(假设没有一种情况是特殊情况,并且需要进行逻辑比较),我是否正确? 一点都不。随时可以使用“ 14”语句。
值必须是常量,但可以是表达式,并经过相等性测试。编译器会告诉您是否做一些愚蠢的事情,例如尝试两次使用相同的
值。
久纳禾澎
鞋扣蚊冈借
3-我不确定您是否根本需要所有价值。我将其删除。 4-是的,对于Flags枚举,switch语句通常没有意义。
缉康怪
臂哦