简体   繁体   English

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

[英]ConfigurationManager - Same App.config structure different projects - Sharing the class

Solved as below!! 解决如下! - I have multiple WPF projects with individual App.Config with custom sections. -我有多个WPF项目,其中每个App.Config都有自定义部分。 All custom sections have the same structure. 所有自定义部分都具有相同的结构。

For one project, I used ConfigurationManager and created Custom ConfigurationSection, ConfigurationCollection, ConfigurationElement and everything works fine for that project. 对于一个项目,我使用了ConfigurationManager并创建了Custom ConfigurationSection,ConfigurationCollection,ConfigurationElement,并且对于该项目来说一切正常。

Then I moved my custom configuration classes in a class library so that I can use them in all the projects, but now I am getting 'System.TypeInitializationException' error when I run the project. 然后,我将自定义配置类移到类库中,以便可以在所有项目中使用它们,但是现在在运行项目时出现“ System.TypeInitializationException”错误。 This seems to be because now ConfigurationManager can't locate the App. 这似乎是因为现在ConfigurationManager找不到应用程序。

I can copy paste the class in all the projects and it works fine but I don't want to do that. 我可以在所有项目中复制并粘贴该类,并且效果很好,但是我不想这样做。 May be I am probably missing something obvious. 可能是我可能缺少明显的东西。 Any help is very much appreciated. 很感谢任何形式的帮助。 Thanks!!! 谢谢!!!

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"]; }
    }        
    }
}

Edit -- App.config file - 编辑-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>

Question rephrased - So, 问题改写-所以,

1) I have multiple app.config for various projects in the structure mentioned above 1)在上述结构中,我有多个app.config用于各种项目

2) I have created custom configuration classes as shown in the code above 2)我已经创建了自定义配置类,如上面的代码所示

I need to put them in a class library\\shared library so that I can reuse the classes instead of copy-pasting them in individual projects. 我需要将它们放在类库\\共享库中,以便可以重用这些类,而不是将其复制粘贴到各个项目中。 When I put the classes in shared library, the project rebuilds fine but it fails when I run it. 当我将类放在共享库中时,项目可以很好地重建,但是在运行时失败。

Answer - Clearly I need to get the basics right. 答案-显然我需要正确的基础知识。 After shifting the code to class library, I had to update app.config accordingly as the namespace and location for the class has now changed. 将代码转移到类库之后,由于类的名称空间和位置现已更改,因此我必须相应地更新app.config。 Sorry for the inconvenience. 抱歉给你带来不便。 Basically, I needed to update "type" of the section as the class now belongs to different assembly. 基本上,我需要更新该部分的“类型”,因为该类现在属于不同的程序集。

Answer - Clearly I need to get the basics right. 答案-显然我需要正确的基础知识。 After shifting the code to class library, I had to update app.config accordingly as the namespace and location for the class has now changed. 将代码转移到类库之后,由于类的名称空间和位置现已更改,因此我必须相应地更新app.config。 Sorry for the inconvenience. 抱歉给你带来不便。 Basically, I needed to update "type" of the section as the class now belongs to different assembly. 基本上,我需要更新该部分的“类型”,因为该类现在属于不同的程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM