0

I am new in C# custom configuration.

I want to do a simple example. I tried this: https://stackoverflow.com/a/14890095/6121574 But, I access configuration file like this: https://stackoverflow.com/a/25806445/6121574

Now I get An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll ...Could not load file or assembly My.Assembly

My question is: What is My.Assembly in App.config file? How to make my code work?

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);
            }

        }
    }
}

My 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>
fr0ga
  • 333
  • 4
  • 9

2 Answers2

1

The first part of the string for the type attribute is the type itself, and that is followed by the assembly that contains the type.

If your type was Company.Project.Configuration.Settings, and that was kept in an assembly of Company.Project.dll, then you would use "Company.Project.Configuration.Settings, Company.Project"

Trioj
  • 303
  • 2
  • 8
0

If the Project name is "My". It works with this config. After 3 hours I found a solution :)

 <?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>
fr0ga
  • 333
  • 4
  • 9