[英]ConfigurationManager - Same App.config structure different projects - Sharing the class
解決如下! -我有多個WPF項目,其中每個App.Config都有自定義部分。 所有自定義部分都具有相同的結構。
對於一個項目,我使用了ConfigurationManager並創建了Custom 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。 抱歉給你帶來不便。 基本上,我需要更新該部分的“類型”,因為該類現在屬於不同的程序集。
答案-顯然我需要正確的基礎知識。 將代碼轉移到類庫之后,由於類的名稱空間和位置現已更改,因此我必須相應地更新app.config。 抱歉給你帶來不便。 基本上,我需要更新該部分的“類型”,因為該類現在屬於不同的程序集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.