解决如下!!- 我有多个 WPF 项目,其中包含带有自定义部分的单独 App.Config。所有自定义部分都具有相同的结构。
对于一个项目,我使用了 ConfigurationManager 并创建了自定义 ConfigurationSection、ConfigurationCollection、ConfigurationElement,并且该项目的一切正常。
然后我将我的自定义配置类移动到一个类库中,以便我可以在所有项目中使用它们,但现在我在运行项目时收到“System.TypeInitializationException”错误。这似乎是因为现在 ConfigurationManager 无法找到该 App。
我可以在所有项目中复制粘贴该类,它工作正常,但我不想这样做。可能是我可能遗漏了一些明显的东西。很感谢任何形式的帮助。谢谢!!!
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,因为类的命名空间和位置现在已经改变。带来不便敬请谅解。基本上,我需要更新该部分的“类型”,因为该类现在属于不同的程序集。