简体   繁体   English

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

[英]C#/.NET 4.0 Creating custom configElements and configCollection

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 为ScriptObjects创建配置节处理程序时发生错误:无法从程序集“ System.Configuration,版本= 4.0.0.0,区域性=中性”加载类型“ ScriptObjectSection”

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. 我已经在网上搜索并找到了MSDN的示例,但是对于我来说,它有点神秘。

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. 您应该在CodeProject上查看Jon Rista的有关.NET 2.0配置的三部分系列。

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: 查看您的代码,最可能的原因是您需要完全限定 config节定义中的config节:

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

The type= attribute needs to be in the form of namespace.ClassName,assemblyName type=属性必须采用namespace.ClassName,assemblyName的形式namespace.ClassName,assemblyName

Your section declaration: 您的部分声明:

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

... incorrectly declares the type as ScriptObjectSection . ...错误地将类型声明为ScriptObjectSection The System.Configuration.dll is the executing assembly that attempts to find the type of ScriptObjectSection , and because you haven't specified namespace and assembly, it cannot find the type. System.Configuration.dll是正在执行的程序集,它将尝试查找ScriptObjectSection的类型,并且由于尚未指定名称空间和程序集,因此无法找到该类型。 Change the section declaration to your full assembly declaration (optionally with strong name arguments): 将节声明更改为完整的程序集声明(可选使用强名称参数):

<section name="ScriptObjects" type="My.Namespace.ScriptObjectSection, MyAssembly" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM