Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EnumMemberAttribute的帮助?_C#_Asp.net - Fatal编程技术网

C# EnumMemberAttribute的帮助?

C# EnumMemberAttribute的帮助?,c#,asp.net,C#,Asp.net,我有一个枚举,下面我需要能够通过传递引用 仅在投保范围内。当我这样做的时候,它说它不能被找到 实际枚举显示为仅保险。还有什么我可以进去的吗 正确的价值,即仅投保而非仅投保 Insured Only是一个字符串,其中InsuredOnly是一个枚举。即使你做了一个.ToString,它也只会被保险 这里有几个选项,举几个例子: 1使用字典映射值 2使用[Descriptionmy description] 3在枚举中使用某种标记,例如下划线,并使用字符串。替换仅保险是字符串,其中仅保险是枚举。即使

我有一个枚举,下面我需要能够通过传递引用 仅在投保范围内。当我这样做的时候,它说它不能被找到 实际枚举显示为仅保险。还有什么我可以进去的吗 正确的价值,即仅投保而非仅投保


Insured Only是一个字符串,其中InsuredOnly是一个枚举。即使你做了一个.ToString,它也只会被保险

这里有几个选项,举几个例子: 1使用字典映射值 2使用[Descriptionmy description]
3在枚举中使用某种标记,例如下划线,并使用字符串。替换仅保险是字符串,其中仅保险是枚举。即使你做了一个.ToString,它也只会被保险

这里有几个选项,举几个例子: 1使用字典映射值 2使用[Descriptionmy description] 3在枚举中使用某种标记,例如下划线,并使用字符串。替换

您可以通过反射来执行此操作。您没有对代码进行任何广泛的测试,但希望您能理解:

private static T GetEnumValue<T>(string description)
{
    // get all public static fields from the enum type
    FieldInfo[] ms = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static);
    foreach (FieldInfo field in ms)
    {
        // pull out the DescriptionAttribute (if any) from the field
        var descriptionAttribute = field
            .GetCustomAttributes(typeof(DescriptionAttribute), true)
            .FirstOrDefault();
        // Check if there was a DescriptionAttribute, and if the 
        // description matches
        if (descriptionAttribute != null
            && (descriptionAttribute as DescriptionAttribute).Description
                    .Equals(description, StringComparison.OrdinalIgnoreCase))
        {
            // return the field value
            return (T)field.GetValue(null);
        }
    }
    return default(T);
}
您可以通过反射来实现这一点,因为您没有对代码进行任何广泛的测试,但希望您能够理解:

private static T GetEnumValue<T>(string description)
{
    // get all public static fields from the enum type
    FieldInfo[] ms = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static);
    foreach (FieldInfo field in ms)
    {
        // pull out the DescriptionAttribute (if any) from the field
        var descriptionAttribute = field
            .GetCustomAttributes(typeof(DescriptionAttribute), true)
            .FirstOrDefault();
        // Check if there was a DescriptionAttribute, and if the 
        // description matches
        if (descriptionAttribute != null
            && (descriptionAttribute as DescriptionAttribute).Description
                    .Equals(description, StringComparison.OrdinalIgnoreCase))
        {
            // return the field value
            return (T)field.GetValue(null);
        }
    }
    return default(T);
}

下面是一个方便的通用方法,它可以满足您的需要:

public T Parse<T>(string description) {
    foreach (FieldInfo field in typeof(T).GetFields()) {
        object[] attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if ((attributes.Length > 0)
            && ((DescriptionAttribute)attributes[0]).Description.Equals(description)
        ) return (T)field.GetRawConstantValue();
    }

    // use default parsing logic if no match is found
    return (T)Enum.Parse(typeof(T), description);
}
用法示例:

EnumNames value = Parse<EnumNames>("Insured Only");

下面是一个方便的通用方法,它可以满足您的需要:

public T Parse<T>(string description) {
    foreach (FieldInfo field in typeof(T).GetFields()) {
        object[] attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if ((attributes.Length > 0)
            && ((DescriptionAttribute)attributes[0]).Description.Equals(description)
        ) return (T)field.GetRawConstantValue();
    }

    // use default parsing logic if no match is found
    return (T)Enum.Parse(typeof(T), description);
}
用法示例:

EnumNames value = Parse<EnumNames>("Insured Only");