Framework2.0中自定义配置节点注意事项

最近在开发自己架构的过程中,在一个配置自定义节点的模块遇到了麻烦。错误代码如下

代码片段

...

代码
  [ConfigurationProperty("mode", DefaultValue = 0)]
        
public SecureWebPageMode Mode
        {
            
get
            {
                
return (SecureWebPageMode)this["mode"] ;
            }
            
set
            {
                
this["mode"]=value;
            }
        }

...

正确代码如下:

代码片段

...

代码
 [ConfigurationProperty("mode", DefaultValue = SecureWebPageMode.On)]
        
public SecureWebPageMode Mode
        {
            
get
            {
                
return (SecureWebPageMode)this["mode"] ;
            }
            
set
            {
                
this["mode"]=value;
            }
        }

...

在ConfigurationProperty特性中设置DefaultValue,枚举值是不能用Int替代的,我认为自定义配置节点微软在类型强转方面有不完美的地方。

作者:Olar Tan
出处:http://www.cnblogs.com/olartan
♪:没有做不到的 只有偷懒而错过的 ♪

原文地址:https://www.cnblogs.com/olartan/p/1622959.html