自定义配置部分似乎加载了错误的文件

时间:2016-09-06 22:09:56

标签: .net configurationsection

您好我正在创建一个库(DLL),我想为其加载.config文件(DLL_name.config)并在其中实现了自定义部分。我使用OpenEXEConfiguration并加载文件,Configuration对象具有正确的FilePath值(直接指向我的" DLL_name.config"文件),但它包含21个.net部分(system.net, system.web等)但不是我的自定义部分。请注意,我的配置文件中出现的唯一部分是我的自定义部分,所以我不明白为什么文件路径看起来没问题,但内容不是该路径中指定的文件中的内容O_o

这是我的配置文件内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="AcceptedStereotypesSection" type="EAReferentialAddInConfigs.CustomConfigs.IntegrityCheck.AcceptedStereotypesSection, EAReferentialAddInConfigs"/>
  </configSections>
  <AcceptedStereotypesSection>
    <AcceptedStereotypes>
      <AcceptedStereotype Stereotype="Test1">
      </AcceptedStereotype>
    </AcceptedStereotypes>
  </AcceptedStereotypesSection>
</configuration>

这是我的自定义栏目类:

public class AcceptedStereotypesSection: ConfigurationSection
{
    [ConfigurationProperty("AcceptedStereotypes", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
    public AcceptedStereotypeCollection AcceptedStereotypes
    {
        get
        {
            return base["AcceptedStereotypes"] as AcceptedStereotypeCollection;
        }

        set
        {
            base["AcceptedStereotypes"] = value;
        }
    }
}

public class AcceptedStereotypeCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new AcceptedStereotype();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((AcceptedStereotype)element).Stereotype;
    }

    protected override string ElementName
    {
        get
        {
            return "AcceptedStereotype";
        }
    }

    protected override bool IsElementName(string elementName)
    {
        return !String.IsNullOrEmpty(elementName) && elementName == ElementName;
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    public AcceptedStereotype this[int index]
    {
        get
        {
            return base.BaseGet(index) as AcceptedStereotype;
        }
    }

    public new AcceptedStereotype this[string key]
    {
        get
        {
            return base.BaseGet(key) as AcceptedStereotype;
        }
    }
}

public class AcceptedStereotype:ConfigurationElement
{
    [ConfigurationProperty("Stereotype", IsKey = true)]
    public String Stereotype
    {
        get
        {
            return (String)this["Stereotype"];
        }
    }

    [ConfigurationProperty("AcceptedRelationships", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
    public AcceptedRelationshipCollection AcceptedRelationships
    {
        get
        {
            return base["AcceptedRelationships"] as AcceptedRelationshipCollection;
        }

        set
        {
            base["AcceptedRelationships"] = value;
        }
    }

}

我已经尝试过两次

Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config" };
        Configuration config2 = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

但总是得到相同的结果:filepath没问题,但内容不代表我上面的自定义部分...

请注意,此DLL用作扩展名为Sparx Enterprise Architect的产品的插件,因此必须注册COM interop ... Dunno为什么会有所作为但仍然想提及它以防万一:)

任何关于为什么会出现这种奇怪行为的想法都将非常感激。我总是可以手动打开配置文件并浏览XML,但这不是一个非常优雅的解决方案,宁可避免使用它......

由于

0 个答案:

没有答案