使用 Flag 属性时的枚举问题
Enumeration issue when using Flag attribute
我有以下枚举:
[Flags]
public enum ElementsTag
{
None,
Surname,
SecondSurname,
Forenames,
PersonalNumber,
Birthday,
Nationality,
DocumentExpirationDate,
DocumentNumber,
Sex,
CityOfBirth,
ProvinceOfBirth,
ParentsName,
PlaceOfResidence,
CityOfResidence,
ProvinceOfResidence
}
因此,当我尝试将枚举值作为参数传递给方法时,如下所示:
this.GetDataElementFromByteArray((byte[])aData, ElementsTag.ParentsName);
我可以在调试中看到 ElementsTag.ParentsName 包含值:
PersonalNumber | DocumentNumber
而不是仅包含 ParentsName。它也发生在枚举的其他成员上,例如,传递给方法 ElementsTag.Nationality 包含:
Nationality = SecondSurname | PersonalNumber
为什么?
我希望每个枚举成员只包含自己的值而不包含其他值,例如:
ElementsTag.ParentsName = ParentsName
ElementsTag.Nationality = Nationality
如何做到这一点?
你的枚举定义与这个相同
[Flags]
public enum ElementsTag
{
None = 0,
Surname = 1,
SecondSurname = 2,
Forenames = 3,
PersonalNumber = 4,
Birthday = 5,
Nationality = 6,
DocumentExpirationDate = 7,
DocumentNumber = 8,
Sex = 9,
CityOfBirth = 10,
ProvinceOfBirth = 11,
ParentsName = 12,
PlaceOfResidence = 13,
CityOfResidence = 14,
ProvinceOfResidence = 15
}
如果您传递 ElementsTag.ParentsName
,则使用值 12。在二进制表示法中,12 = 0000 1100。因此设置了第 3 位和第 4 位。第 3 位对应值 4,即 ElementsTag.PersonalNumber
,第 4 位是值 8,对应 ElementsTag.DocumentNumber
.
如果你想要不同的值,你必须像这样使用 2^n 个值:
[Flags]
public enum ElementsTag
{
None = 0,
Surname = 1,
SecondSurname = 1 << 1, // 2
Forenames = 1 << 2, // 4
PersonalNumber = 1 << 3, // 8
Birthday = 1 << 4,
Nationality = 1 << 5,
DocumentExpirationDate = 1 << 6,
DocumentNumber = 1 << 7,
Sex = 1 << 8,
CityOfBirth = 1 << 9,
ProvinceOfBirth = 1 << 10,
ParentsName = 1 << 11,
PlaceOfResidence = 1 << 12,
CityOfResidence = 1 << 13,
ProvinceOfResidence = 1 << 14
}
我有以下枚举:
[Flags]
public enum ElementsTag
{
None,
Surname,
SecondSurname,
Forenames,
PersonalNumber,
Birthday,
Nationality,
DocumentExpirationDate,
DocumentNumber,
Sex,
CityOfBirth,
ProvinceOfBirth,
ParentsName,
PlaceOfResidence,
CityOfResidence,
ProvinceOfResidence
}
因此,当我尝试将枚举值作为参数传递给方法时,如下所示:
this.GetDataElementFromByteArray((byte[])aData, ElementsTag.ParentsName);
我可以在调试中看到 ElementsTag.ParentsName 包含值:
PersonalNumber | DocumentNumber
而不是仅包含 ParentsName。它也发生在枚举的其他成员上,例如,传递给方法 ElementsTag.Nationality 包含:
Nationality = SecondSurname | PersonalNumber
为什么?
我希望每个枚举成员只包含自己的值而不包含其他值,例如:
ElementsTag.ParentsName = ParentsName
ElementsTag.Nationality = Nationality
如何做到这一点?
你的枚举定义与这个相同
[Flags]
public enum ElementsTag
{
None = 0,
Surname = 1,
SecondSurname = 2,
Forenames = 3,
PersonalNumber = 4,
Birthday = 5,
Nationality = 6,
DocumentExpirationDate = 7,
DocumentNumber = 8,
Sex = 9,
CityOfBirth = 10,
ProvinceOfBirth = 11,
ParentsName = 12,
PlaceOfResidence = 13,
CityOfResidence = 14,
ProvinceOfResidence = 15
}
如果您传递 ElementsTag.ParentsName
,则使用值 12。在二进制表示法中,12 = 0000 1100。因此设置了第 3 位和第 4 位。第 3 位对应值 4,即 ElementsTag.PersonalNumber
,第 4 位是值 8,对应 ElementsTag.DocumentNumber
.
如果你想要不同的值,你必须像这样使用 2^n 个值:
[Flags]
public enum ElementsTag
{
None = 0,
Surname = 1,
SecondSurname = 1 << 1, // 2
Forenames = 1 << 2, // 4
PersonalNumber = 1 << 3, // 8
Birthday = 1 << 4,
Nationality = 1 << 5,
DocumentExpirationDate = 1 << 6,
DocumentNumber = 1 << 7,
Sex = 1 << 8,
CityOfBirth = 1 << 9,
ProvinceOfBirth = 1 << 10,
ParentsName = 1 << 11,
PlaceOfResidence = 1 << 12,
CityOfResidence = 1 << 13,
ProvinceOfResidence = 1 << 14
}