2017-11-03 23 views
0

C#のカスタム設定では新しいです。App.configカスタムセクションのタイプ

私は簡単な例をしたいと思います。 私はこの試みた:https://stackoverflow.com/a/14890095/6121574 をしかし、私はこのようなコンフィギュレーションファイルにアクセス:https://stackoverflow.com/a/25806445/6121574

今、私はタイプ「System.Configuration.ConfigurationErrorsException」の 未処理の例外がSystem.Configuration.dll で発生し得ることができませんでした...ファイルまたはアセンブリをロードするMy.Assembly

質問:My.AssemblyとApp.configファイルは何ですか?私のコードを動作させるには?

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> 

答えて

0

プロジェクト名が "My"の場合。この設定で動作します。 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> 
0

type属性の文字列の最初の部分は、型自体であり、それはタイプを含むアセンブリが続いています。あなたのタイプはCompany.Project.Configuration.Settingsだった、そしてそれはCompany.Project.dllの組み立て中に保持した場合、あなたが使用し

「Company.Project.Configuration.Settingsを、Company.Project」

関連する問題