Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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# .net自定义配置如何不区分大小写地分析enum ConfigurationProperty_C#_.net_Configurationproperty - Fatal编程技术网

C# .net自定义配置如何不区分大小写地分析enum ConfigurationProperty

C# .net自定义配置如何不区分大小写地分析enum ConfigurationProperty,c#,.net,configurationproperty,C#,.net,Configurationproperty,我的ConfigurationSection中的ConfigurationProperty之一是枚举。当.net从配置文件解析此枚举字符串值时,如果大小写不完全匹配,将引发异常 在分析此值时是否可以忽略大小写?尝试使用以下方法: Enum.Parse(enum_type, string_value, true); 最后一个参数设置为true表示在分析时忽略字符串大小写。MyEnum.TryParse()有一个IgnoreCase参数,请将其设置为true 更新: 像这样定义配置部分应该是可行

我的ConfigurationSection中的ConfigurationProperty之一是枚举。当.net从配置文件解析此枚举字符串值时,如果大小写不完全匹配,将引发异常

在分析此值时是否可以忽略大小写?

尝试使用以下方法:

Enum.Parse(enum_type, string_value, true);
最后一个参数设置为true表示在分析时忽略字符串大小写。

MyEnum.TryParse()
有一个IgnoreCase参数,请将其设置为true

更新: 像这样定义配置部分应该是可行的

public class CustomConfigurationSection : ConfigurationSection
    {
      [ConfigurationProperty("myEnumProperty", DefaultValue = MyEnum.Item1, IsRequired = true)]
      public MyEnum SomeProperty
      {
        get
        {
          MyEnum tmp;
          return Enum.TryParse((string)this["myEnumProperty"],true,out tmp)?tmp:MyEnum.Item1;
        }
        set
        { this["myEnumProperty"] = value; }
      }
    }

您可以使用ConfigurationConverterBase创建自定义配置转换器,请参阅

这将完成以下工作:

 public class CaseInsensitiveEnumConfigConverter<T> : ConfigurationConverterBase
    {
        public override object ConvertFrom(
        ITypeDescriptorContext ctx, CultureInfo ci, object data)
        {
            return Enum.Parse(typeof(T), (string)data, true);
        }
    }
公共类CaseInsensitiveEnumConfigConverter:ConfigurationConverterBase
{
公共重写对象转换自(
ITypeDescriptorContext ctx、CultureInfo ci、对象数据)
{
返回Enum.Parse(typeof(T),(string)数据,true);
}
}
然后在你的财产上:

[ConfigurationProperty("unit", DefaultValue = MeasurementUnits.Pixel)]
[TypeConverter(typeof(CaseInsensitiveEnumConfigConverter<MeasurementUnits>))]
public MeasurementUnits Unit { get { return (MeasurementUnits)this["unit"]; } }

public enum MeasurementUnits
{
        Pixel,
        Inches,
        Points,
        MM,
}
[ConfigurationProperty(“单位”,DefaultValue=MeasurementUnits.Pixel)]
[TypeConverter(typeof(CaseInsensitiveEnumConfigConverter))]
公共度量单位{get{return(度量单位)this[“Unit”];}
公共枚举度量
{
像素
英寸,
要点,,
嗯,
}

Enum.Parse
接受一个布尔值,告诉它忽略大小写。@teddy只有当Enum成员也都是大写时才有用……是的,我知道Enum.Parse有一个ignorecase标志。但是,当我使用ConfigurationPropertyAttribute时,.net会自动解析此ConfigurationProperty。是的,我知道Enum.Parse有一个ignorecase标志。但是,当我使用ConfigurationPropertyAttribute时,.net会自动解析此ConfigurationProperty。谢谢。这解决了我的问题,而无需编写其他代码。请阅读Koda对原始问题的评论。它使用ConfigurationPropertyAttribute,它在区分大小写模式下自动解析。不直接使用Enum.Parse。接受的答案(从ConfigurationConvertorBase继承)是正确的答案。@MattSach我想是对错误问题的正确答案。遗憾的是,这是Google上“.net不区分大小写的enum parse”的首批几个结果之一。注意:enum.TryParse有一个带有相同参数的签名,同样有效