首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConfigurationManager -相同的App.config结构不同的项目-共享类

ConfigurationManager -相同的App.config结构不同的项目-共享类
EN

Stack Overflow用户
提问于 2014-04-29 00:46:25
回答 1查看 376关注 0票数 0

解决如下!!-我有多个独立的App.Config项目与自定义部分。所有自定义部分都具有相同的结构。

对于一个项目,我使用了ConfigurationManager并创建了自定义的ConfigurationSection、ConfigurationCollection、ConfigurationElement,该项目一切正常。

然后我将我的自定义配置类移动到一个类库中,这样我就可以在所有项目中使用它们,但现在我在运行项目时收到了'System.TypeInitializationException‘错误。这似乎是因为现在ConfigurationManager找不到应用程序了。

我可以在所有项目中复制粘贴类,它工作得很好,但我不想这样做。也许我可能遗漏了一些明显的东西。任何帮助都是非常感谢的。谢谢!

代码语言:javascript
复制
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文件-

代码语言:javascript
复制
<?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,因为类的命名空间和位置现在已经更改。很抱歉给您带来不便。基本上,我需要更新部分的“类型”,因为类现在属于不同的程序集。

EN

回答 1

Stack Overflow用户

发布于 2014-04-29 18:49:33

答案--很明显,我需要做好基本的准备工作。在将代码转移到类库之后,我必须相应地更新app.config,因为类的命名空间和位置现在已经更改。很抱歉给您带来不便。基本上,我需要更新部分的“类型”,因为类现在属于不同的程序集。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23346695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档