【问题标题】:What is type in App.config custom sectionApp.config 自定义部分中的类型是什么
【发布时间】:2018-04-16 13:32:06
【问题描述】:

我是 C# 自定义配置的新手。

我想做一个简单的例子。 我试过这个:https://stackoverflow.com/a/14890095/6121574 但是,我像这样访问配置文件:https://stackoverflow.com/a/25806445/6121574

现在我明白了 System.Configuration.dll 中出现“System.Configuration.ConfigurationErrorsException”类型的未处理异常 ...无法加载文件或程序集 My.Assembly

我的问题是:App.config 文件中的 My.Assembly 是什么?如何让我的代码工作?

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace My
{
    public class MyConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        public MyConfigInstanceCollection Instances
        {
            get { return (MyConfigInstanceCollection)this[""]; }
            set { this[""] = value; }
        }
    }
    public class MyConfigInstanceCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MyConfigInstanceElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            //set to whatever Element Property you want to use for a key
            return ((MyConfigInstanceElement)element).Name;
        }
    }

    public class MyConfigInstanceElement : ConfigurationElement
    {
        //Make sure to set IsKey=true for property exposed as the GetElementKey above
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("code", IsRequired = true)]
        public string Code
        {
            get { return (string)base["code"]; }
            set { base["code"] = value; }
        }
    }

    class Program
    {


        static void Main(string[] args)
        {
            var config = ConfigurationManager.GetSection("registerCompanies")
                  as MyConfigSection;

            foreach (MyConfigInstanceElement e in config.Instances)
            {
                Console.WriteLine("Name: {0}, Code: {1}", e.Name, e.Code);
            }

        }
    }
}

我的 App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="registerCompanies"
             type="My.MyConfigSection, My.Assembly" />
  </configSections>
  <registerCompanies>
    <add name="Tata Motors" code="Tata"/>
    <add name="Honda Motors" code="Honda"/>
  </registerCompanies>
</configuration>

【问题讨论】:

    标签: c# config app-config


    【解决方案1】:

    类型属性字符串的第一部分是类型本身,后面是包含该类型的程序集。

    如果您的类型是 Company.Project.Configuration.Settings,并且保存在 Company.Project.dll 的程序集中,那么您将使用“Company.Project.Configuration.Settings, Company.Project”

    【讨论】:

      【解决方案2】:

      如果项目名称是“我的”。它适用于此配置。 3小时后,我找到了解决方案:)

       <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <configSections>
          <section name="registerCompanies"
                   type="My.MyConfigSection, My" />
        </configSections>
        <registerCompanies>
          <add name="Tata Motors" code="Tata"/>
          <add name="Honda Motors" code="Honda"/>
        </registerCompanies>
      </configuration>
      

      【讨论】:

        猜你喜欢
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-04
        • 2012-04-05
        • 2011-01-30
        • 2011-10-02
        相关资源
        最近更新 更多