C#/。NET 4.0创建自定义configElements和configCollection自定义、NET、configElements、configCollection

2023-09-03 22:28:42 作者:男女都可以用的 男女通用的好听的

想创建一个自定义的配置元素如下所示:

 < XML版本=1.0编码=UTF-8&GT?;
<结构>
  < configSections>
    <节名称=ScriptObjectsTYPE =ScriptObjectSection/>
  < / configSections>
  < ScriptObjects>
    <用户>
      <使用者名称=贾斯汀/>
    < /用户>
  < / ScriptObjects>
< /结构>
 

不过,我得到这个错误:

  

发生创造了配置节处理程序错误   ScriptObjects:未能从程序集加载类型ScriptObjectSection   System.Configuration,版本= 4.0.0.0,文化=中性

我想有过的所有用户元素循环的​​能力。

我已经搜查了网,发现一个样品从MSDN,但它是一个稍微有点神秘,我明白了。

下面是code我有一个自定义元素,元素集合,并配置部分。

任何人不会对此有什么指导意见?我在哪里搞乱?

 公共类ScriptObjectSection:配置节
{
    [的ConfigurationProperty(用户)]
    [ConfigurationCollection(typeof运算(UserCollection))]
    公共UserCollection用户
    {
        {返回(UserCollection)基地[用户]; }
    }
}

公共类UserElement:的ConfigurationElement
{
    [的ConfigurationProperty(名称)]
    公共字符串名称
    {
        {返回(串)本[名称]; }
    }
}

公共类UserCollection:ConfigurationElementCollection
{
    公众覆盖ConfigurationElementCollectionType CollectionType
    {
        得到
        {
            返回ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

    保护覆盖的ConfigurationElement CreateNewElement()
    {
        抛出新的NotImplementedException();
    }

    保护覆盖对象GetElementKey(的ConfigurationElement元)
    {
        抛出新的NotImplementedException();
    }

    公共UserElement这个[INT指数]
    {
        {返回(UserElement)BaseGet(指数); }
    }
}
 

该错误是扔在这条线:

  ScriptObjectSection SOS = ConfigurationManager.GetSection(ScriptObjects)作为ScriptObjectSection;
 
如何创建自定义数字和条件功能

解决方案

您应该检查出乔恩Rista的三部系列.NET 2.0的配置上$ C $的CProject。

揭开.NET 2.0配置 解码.NET 2.0配置 开裂的.NET 2.0配置

强烈推荐,写得很好,非常有帮助!我学到了很多,从这些文章,并能计算出从研究这些code段我的配置需求。

看你的code,最可能的原因是,你需要为完全限定在配置部分定义您的配置部分:

 < configSections>
    <节名称=ScriptObjectsTYPE =ScriptObjectSection/>
< / configSections>
 

键入= 属性需要在 namespace.ClassName,的AssemblyName

的形式

Trying to create a custom configuration elements as seen below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ScriptObjects" type="ScriptObjectSection" />
  </configSections>
  <ScriptObjects>
    <users>
      <user name="Justin"/>
    </users>
  </ScriptObjects>
</configuration>

But I'm getting this error:

An error occurred creating the configuration section handler for ScriptObjects: Could not load type 'ScriptObjectSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral

I want to have the ability of looping through all user elements.

I've searched the net and found a sample from MSDN, but its a slightly too cryptic for me to understand.

Below is the code I have for the custom element, element collection, and config section.

Does any one have any guidance on this? Where am I messing up?

public class ScriptObjectSection : ConfigurationSection
{
    [ConfigurationProperty("Users")]
    [ConfigurationCollection(typeof(UserCollection))]
    public UserCollection Users
    {
        get { return (UserCollection)base[ "users" ]; }
    }
}

public class UserElement : ConfigurationElement
{
    [ConfigurationProperty( "name" )]
    public string Name
    {
        get { return (string)this[ "name" ]; }
    }
}

public class UserCollection : ConfigurationElementCollection
{
    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        throw new NotImplementedException();
    }

    protected override object GetElementKey( ConfigurationElement element )
    {
        throw new NotImplementedException();
    }

    public UserElement this[ int index ]
    {
        get { return (UserElement)BaseGet( index ); }
    }
}

The Error is thrown on this line:

 ScriptObjectSection sos = ConfigurationManager.GetSection("ScriptObjects") as ScriptObjectSection;

解决方案

You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

Unraveling the mysteries of .NET 2.0 configuration Decoding the mysteries of .NET 2.0 configuration Cracking the mysteries of .NET 2.0 configuration

Highly recommended, well written and extremely helpful! I learned a lot from those articles and was able to figure out my config needs from studying those code snippets.

Looking at your code, the most probable cause is that you need to fully qualify your config section in the config section definition:

<configSections>
    <section name="ScriptObjects" type="ScriptObjectSection" />
</configSections>

The type= attribute needs to be in the form of namespace.ClassName,assemblyName