Lambda中的条件有两个值

时间:2022-12-10 13:13:47
public enum Values
{
    [Description("All Fabs")]
    value1 = 0,
    [Description("Fab 1")]
    value2 = 1,
    [Description("Fab 2")]
    value3 = 2,
    [Description("Fab 3")]
    value4 = 3,
    [Description("Fab 4")]
    value5 = 4,
    [Description("Fab 5")]
    value6 = 5           
}

public static Dictionary<int, string> ConvertEnumToDictionary<T>()
{
    var type = typeof(T);
    if(!type.IsEnum)
    {
        throw new InvalidOperationException();
    }
    Dictionary<int, string> result = new Dictionary<int, string>();
    bool header = true;
    foreach(var field in type.GetFields())
    {
        if(header)
        {
            header = false;
            continue;
        }
        var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
        if(attribute != null)
        {
            result.Add((int)field.GetValue(null), attribute.Description);
        }
    }
    return result;
}

Displays.ConvertEnumToDictionary<Values>().Where(i => (i.Key == value2 || i.Key == value1));

Displays.ConvertEnumToDictionary ()。Where(i =>(i.Key == value2 || i.Key == value1));

the above line produces diff results in development environment and in staging environment.

上面的行在开发环境和登台环境中产生差异结果。

In development it returns both the values but in staging sometimes it returns one value only (either value1 or value2) and sometimes both.

在开发过程中,它返回两个值,但在暂存时,有时它只返回一个值(value1或value2),有时两者都返回。

Please help me find out the problem here.

请帮我解决这个问题。

2 个解决方案

#1


3  

It may be because of this sentence in the documentation:

可能是因为文档中的这句话:

Your code must not depend on the order in which fields are returned, because that order varies.

您的代码不得依赖于返回字段的顺序,因为该顺序会有所不同。

You're skipping the first item you iterate (your header variable). That will only work if the fields come back in the same order every time.

你正在跳过你迭代的第一个项目(你的标题变量)。只有当字段每次都以相同的顺序返回时,这才有效。

Try removing the code that skips the "header", and instead getting the field value into a variable and checking whether value.Equals(default(T)). That would skip the enum value with 0 as its backing integer value.

尝试删除跳过“标题”的代码,然后将字段值转换为变量并检查value.Equals(default(T))。这将跳过枚举值,其中0作为其后备整数值。

#2


0  

It may be linked to the "header" part while iterating raw fields. Prefer this manner to iterate thru enum values:

在迭代原始字段时,它可以链接到“标题”部分。首选这种方式迭代枚举值:

FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static)

Then you can start at offset-0:

然后你可以从offset-0开始:

for (int i = 0; i < fields.Length; i++)

#1


3  

It may be because of this sentence in the documentation:

可能是因为文档中的这句话:

Your code must not depend on the order in which fields are returned, because that order varies.

您的代码不得依赖于返回字段的顺序,因为该顺序会有所不同。

You're skipping the first item you iterate (your header variable). That will only work if the fields come back in the same order every time.

你正在跳过你迭代的第一个项目(你的标题变量)。只有当字段每次都以相同的顺序返回时,这才有效。

Try removing the code that skips the "header", and instead getting the field value into a variable and checking whether value.Equals(default(T)). That would skip the enum value with 0 as its backing integer value.

尝试删除跳过“标题”的代码,然后将字段值转换为变量并检查value.Equals(default(T))。这将跳过枚举值,其中0作为其后备整数值。

#2


0  

It may be linked to the "header" part while iterating raw fields. Prefer this manner to iterate thru enum values:

在迭代原始字段时,它可以链接到“标题”部分。首选这种方式迭代枚举值:

FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static)

Then you can start at offset-0:

然后你可以从offset-0开始:

for (int i = 0; i < fields.Length; i++)