【发布时间】:2014-06-14 07:55:52
【问题描述】:
如下解决!! - 我有多个 WPF 项目,其中包含带有自定义部分的单独 App.Config。所有自定义部分都具有相同的结构。
对于一个项目,我使用了 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,因为类的命名空间和位置现在已经改变。带来不便敬请谅解。基本上,我需要更新该部分的“类型”,因为该类现在属于不同的程序集。
【问题讨论】:
-
如果你不打算共享代码,至少要共享堆栈跟踪。
-
除非是 GAC 程序集,否则您仍然需要 .dll。
-
对不起,我已经添加了我想在上面的类库\共享库中使用的配置代码。如果我将整个代码作为 projectA 的一部分包含在内,它工作正常。即当我访问 - projectA 中的 CustomConfig.Settings.Categories["mycategory"] 时,代码工作正常。但是如果我把它分开放在类库(classLibraryA)中,我就不能再在projectA中使用CustomConfig.Settings.Categories["mycategory"]了。
标签: c# app-config class-library configurationmanager custom-configuration