0

以下で解決!! - カスタム セクションを含む個別の App.Config を含む複数の WPF プロジェクトがあります。すべてのカスタム セクションの構造は同じです。

あるプロジェクトでは、ConfigurationManager を使用し、カスタム ConfigurationSection、ConfigurationCollection、ConfigurationElement を作成しました。そのプロジェクトではすべてが正常に機能します。

次に、カスタム構成クラスをクラス ライブラリに移動して、すべてのプロジェクトで使用できるようにしましたが、プロジェクトを実行すると「System.TypeInitializationException」エラーが発生します。これは、ConfigurationManager がアプリを見つけることができないためと思われます。

すべてのプロジェクトでクラスをコピーして貼り付けることができ、正常に動作しますが、そうしたくありません。おそらく明らかな何かが欠けているのかもしれません。どんな助けでも大歓迎です。ありがとう!!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;

namespace WordAddinForms
{

    public class CustomConfig : ConfigurationSection
    {
    public static readonly CustomConfig Settings =
        (CustomConfig)ConfigurationManager.GetSection("custom-configuration"); 

    [ConfigurationProperty("activities")]
    public ActivityElementCollection Activities
    {
        get { return (ActivityElementCollection)base["activities"]; }
    }       
    }

    [ConfigurationCollection(typeof(ActivityElement), AddItemName = "activity",
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
    public class ActivityElementCollection : ConfigurationElementCollection, IEnumerable<ActivityElement>
    {
    IEnumerator<ActivityElement> IEnumerable<ActivityElement>.GetEnumerator()
    {
        return this.OfType<ActivityElement>().GetEnumerator();
    }
    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }
    protected override string ElementName
    {
        get { return "activity"; }
    }
    protected override ConfigurationElement CreateNewElement()
    {
        return new ActivityElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return (element as ActivityElement).Name;
    }

    public ActivityElement this[int index]
    {
        get { return (ActivityElement)base.BaseGet(index); }
        set
        {
        if (base.BaseGet(index) != null)
        {
            base.BaseRemoveAt(index);
        }
        base.BaseAdd(index, value);
        }
    }
    public ActivityElement this[string name]
    {
        get { return (ActivityElement)base.BaseGet(name); }
    }

    }

    public class ActivityElement : ConfigurationElement
    {
    [ConfigurationProperty("name", DefaultValue = "String.Empty")]
    public string Name
    {
        get { return (string)base["name"]; }
    }        
    [ConfigurationProperty("location", DefaultValue = "String.Empty")]
    public string Location
    {
        get { return (string)base["location"]; }
    }       

    [ConfigurationProperty("files")]
    public FileElementCollection Files
    {
        get { return (FileElementCollection)base["files"]; }
    }
    }

    [ConfigurationCollection(typeof(FileElement), AddItemName = "file",
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
    public class FileElementCollection : ConfigurationElementCollection, IEnumerable<FileElement>
    {
    IEnumerator<FileElement> IEnumerable<FileElement>.GetEnumerator()
    {
        return this.OfType<FileElement>().GetEnumerator();
    }
    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }
    protected override string ElementName
    {
        get { return "file"; }
    }
    protected override ConfigurationElement CreateNewElement()
    {
        return new FileElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return (element as FileElement).Name;
    }

    public FileElement this[int index]
    {
        get { return (FileElement)base.BaseGet(index); }
        set
        {
        if (base.BaseGet(index) != null)
        {
            base.BaseRemoveAt(index);
        }
        base.BaseAdd(index, value);
        }
    }
    public FileElement this[string name]
    {
        get { return (FileElement)base.BaseGet(name); }
    }
    }

    public class FileElement : ConfigurationElement
    {
    [ConfigurationProperty("name", DefaultValue = "String.Empty")]
    public string Name
    {
        get { return (string)base["name"]; }
    }

    /// <remarks />
    [ConfigurationProperty("location", DefaultValue = "String.Empty")]
    public string Location
    {
        get { return (string)base["location"]; }
    }        
    }
}

編集 -- App.config ファイル --

<?xml version="1.0" ?>
<custom-configuration>
 <activities>
  <activity name="Activities" location=".\Activity\">
    <files>
      <file name="Running" location=".Running\"/>
      <file name="Sports" location=".Sports\"/>
      <file name="Fun" location=".Fun\"/>
      <file name="Exercise" location=".Exercise\"/>     
    </files>
  </activity>  
 </activities>
</custom-configuration>

質問の言い換え - だから、

1)上記の構造のさまざまなプロジェクトに複数の app.config があります

2)上記のコードに示すように、カスタム構成クラスを作成しました

個々のプロジェクトでクラスをコピーして貼り付けるのではなく、クラスを再利用できるように、それらをクラス ライブラリ \ 共有ライブラリに配置する必要があります。クラスを共有ライブラリに配置すると、プロジェクトは正常に再構築されますが、実行すると失敗します。

回答 - 明らかに、基本を正しく理解する必要があります。コードをクラス ライブラリに移動した後、クラスの名前空間と場所が変更されたため、それに応じて app.config を更新する必要がありました。ご不便おかけしてすみません。基本的に、クラスが別のアセンブリに属しているため、セクションの「タイプ」を更新する必要がありました。

4

1 に答える 1

0

回答 - 明らかに、基本を正しく理解する必要があります。コードをクラス ライブラリに移動した後、クラスの名前空間と場所が変更されたため、それに応じて app.config を更新する必要がありました。ご不便おかけしてすみません。基本的に、クラスが別のアセンブリに属しているため、セクションの「タイプ」を更新する必要がありました。

于 2014-04-29T10:49:32.997 に答える