自定义配置部分的问题
Problems with custom config section
我正在尝试创建一个相当简单的自定义配置部分。我的 class 是:
namespace NetCenterUserImport
{
public class ExcludedUserList : ConfigurationSection
{
[ConfigurationProperty("name")]
public string Name
{
get { return (string)base["name"]; }
}
[ConfigurationProperty("excludedUser")]
public ExcludedUser ExcludedUser
{
get { return (ExcludedUser)base["excludedUser"]; }
}
[ConfigurationProperty("excludedUsers")]
public ExcludedUserCollection ExcludedUsers
{
get { return (ExcludedUserCollection)base["excludedUsers"]; }
}
}
[ConfigurationCollection(typeof(ExcludedUser), AddItemName = "add")]
public class ExcludedUserCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ExcludedUserCollection();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ExcludedUser)element).UserName;
}
}
public class ExcludedUser : ConfigurationElement
{
[ConfigurationProperty("name")]
public string UserName
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}
}
我的 app.config 是:
<excludedUserList name="test">
<excludedUser name="Hello" />
<excludedUsers>
<add name="myUser" />
</excludedUsers>
当我尝试使用以下方式获取自定义配置部分时:
var excludedUsers = ConfigurationManager.GetSection("excludedUserList");
我得到一个例外说
"Unrecognized attribute 'name'."
我确定我遗漏了一些简单的东西,但我已经查看了这里的十几个示例和答案,但似乎找不到哪里出错了。
在 ExcludedUserCollection.CreateNewElement 方法中,您正在创建一个 ExcludedUserCollection 实例,它应该是单个元素,例如:
protected override ConfigurationElement CreateNewElement()
{
return new ExcludedUser();
}
改变上述方法对我有用。
我正在尝试创建一个相当简单的自定义配置部分。我的 class 是:
namespace NetCenterUserImport
{
public class ExcludedUserList : ConfigurationSection
{
[ConfigurationProperty("name")]
public string Name
{
get { return (string)base["name"]; }
}
[ConfigurationProperty("excludedUser")]
public ExcludedUser ExcludedUser
{
get { return (ExcludedUser)base["excludedUser"]; }
}
[ConfigurationProperty("excludedUsers")]
public ExcludedUserCollection ExcludedUsers
{
get { return (ExcludedUserCollection)base["excludedUsers"]; }
}
}
[ConfigurationCollection(typeof(ExcludedUser), AddItemName = "add")]
public class ExcludedUserCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ExcludedUserCollection();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ExcludedUser)element).UserName;
}
}
public class ExcludedUser : ConfigurationElement
{
[ConfigurationProperty("name")]
public string UserName
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}
}
我的 app.config 是:
<excludedUserList name="test">
<excludedUser name="Hello" />
<excludedUsers>
<add name="myUser" />
</excludedUsers>
当我尝试使用以下方式获取自定义配置部分时:
var excludedUsers = ConfigurationManager.GetSection("excludedUserList");
我得到一个例外说
"Unrecognized attribute 'name'."
我确定我遗漏了一些简单的东西,但我已经查看了这里的十几个示例和答案,但似乎找不到哪里出错了。
在 ExcludedUserCollection.CreateNewElement 方法中,您正在创建一个 ExcludedUserCollection 实例,它应该是单个元素,例如:
protected override ConfigurationElement CreateNewElement()
{
return new ExcludedUser();
}
改变上述方法对我有用。