简体   繁体   English

C#枚举从值列表中进行验证

[英]C# Enum validate from list of values

I saw somewhere (cannot find it anymore) that you can check the existence of an enum value from an enum item with a specific list of items. 我在某个地方看到了(无法再找到它),您可以检查带有特定项目列表的枚举项目中是否存在枚举值。 Ex. 例如 below - "Available = doc | xls | csv" 下面-“可用= doc | xls | csv”

But the following code does not seem to work. 但是以下代码似乎不起作用。 I'm expecting the results to be = xls instead of doc, since it is in the list of "Available" values. 我期望结果是= xls而不是doc,因为它在“可用”值列表中。

Can someone please help? 有人可以帮忙吗?

Thanks in advance! 提前致谢!

Niki 尼基

Button Code: 按钮代码:

protected void btnTest01_Click(object sender, EventArgs e)
{
    TestEnum01 result1 = TestEnum01.xls;
    TestEnum02 result2 = TestEnum02.xls;
    TestEnum03 result3 = TestEnum03.xls;

    if (result1 != TestEnum01.Available)
    {
        result1 = TestEnum01.doc;
    }

    if (result2 != TestEnum02.Available)
    {
        result2 = TestEnum02.doc;
    }

    if (result3 != TestEnum03.Available)
    {
        result3 = TestEnum03.doc;
    }

    this.txtTest01_Results.Text =
        String.Format("01: Result = {0}, Available = {1}\r\n\r\n02: Result = {2}, Available = {3}\r\n\r\n03: Result = {4}, Available = {5}",
        result1.ToString(), TestEnum01.Available,
        result2.ToString(), TestEnum02.Available,
        result3.ToString(), TestEnum03.Available);
    }

ENUMS 枚举

public enum TestEnum01
{
    doc = 1,
    txt = 2,
    xls = 4,
    csv = 8,
    unknown = 5,
    Available = doc | xls | csv
}

public enum TestEnum02
{
    doc,
    txt,
    xls,
    csv,
    unknown,
    Available = doc | xls | csv
}

public enum TestEnum03
{
    doc,
    txt,
    xls,
    csv,
    unknown,
    Available = TestEnum03.doc | TestEnum03.xls | TestEnum03.csv
}

RESULTS: 结果:

01: Result = doc, Available = Available
02: Result = doc, Available = csv
03: Result = doc, Available = csv

You have to use the FlagsAttribute : 您必须使用FlagsAttribute

[Flags]
public enum TestEnum01
{
    doc = 1,
    txt = 2,
    xls = 4,
    csv = 8,
    unknown = 5,
    Available = doc | xls | csv
}

Then to test it : 然后测试一下:

TestEnum01 test = TestEnum01.doc | TestEnum01.txt;
bool isDoc = (test & TestEnum01.doc) == TestEnum01.doc;

Note that in your example, you will have a problem with unknown value, since binary wise, 1 | 请注意,在您的示例中,您将遇到一个值unknown的问题,因为二进制明智的方法1 | 4 = 5 ... And that means doc and xls produces unknown... To avoid this kind of problems, I prefer to use a direct bit-shift notation : 4 = 5 ...这意味着doc和xls产生未知数...为了避免此类问题,我更喜欢使用直接的移位符号:

[Flags]
public enum TestEnum01
{
    unknown = 0,
    doc = 1 << 0,
    txt = 1 << 1,
    xls = 1 << 2,
    csv = 1 << 3,
    Available = doc | xls | csv
}

If you just want to test for a particular flag, you can just use the HasFlag() method 如果只想测试特定标志,则可以使用HasFlag()方法

TestEnum01 test = TestEnum01.doc | TestEnum01.txt;
bool isDoc = test.HasFlag(TestEnum01.doc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM