简体   繁体   English

自定义 ConfigurationElementCollection 抛出 TargetInvocationException

[英]Custom ConfigurationElementCollection Throw TargetInvocationException

I'm trying to make a Custom ConfigurationSection with ConfigurationElementCollection .我试图做一个自定义ConfigurationSectionConfigurationElementCollection But when i use it throw a ConfigurationErrorsException with 2 detail exception TargetInvocationException And MissingMethodException但是当我使用它时会抛出一个ConfigurationErrorsException和 2 个细节异常TargetInvocationExceptionMissingMethodException

I console some message in the Constructor, result is just "new AssembySettingSection".我在构造函数中安慰一些消息,结果只是“新的 AssembySettingSection”。 Exception throw at AssembySettingSection base["settings"].在 AssembySettingSection base["settings"] 处抛出异常。

App.config应用配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="AssembySetting" type="IngoWinService.AssembySettingSection, IngoWinService"/>
  </configSections>
  <AssembySetting>
    <settings>
      <add name="IngoWinService.Haier.Service" assemby="IngoWinService.Haier" />
    </settings>
  </AssembySetting>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

Section Code(Post others when need)部分代码(需要时发布其他人)

public class AssembySettingSection : ConfigurationSection
{
    public AssembySettingSection()
    {
        System.Console.WriteLine("new AssembySettingSection");
    }
    [ConfigurationProperty("settings", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(AssembySettingCollection))]
    public ConfigurationElementCollection Settings
    {
        get
        {
            return base["settings"] as AssembySettingCollection;//Throw Exception
        }
    }

}

Sorry, I used the wrong element type in the problem.抱歉,我在问题中使用了错误的元素类型。 Anyway, answering this for other users.无论如何,为其他用户回答这个问题。

The problem is fixed.问题已解决。

public class AssembySettingSection : ConfigurationSection
{
    public AssembySettingSection()
    {
        System.Console.WriteLine("new AssembySettingSection");
    }
    [ConfigurationProperty("settings", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(AssembySettingElement))]//Changed AssembySettingCollection to AssembySettingElement
    public AssembySettingCollection Settings
    {
        get
        {
            return base["settings"] as AssembySettingCollection;
        }
    }

}

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

相关问题 如何在ConfigurationElementCollection中拥有自定义属性? - how to have custom attribute in ConfigurationElementCollection? 单元测试自定义ConfigurationElement和ConfigurationElementCollection - Unit Testing custom ConfigurationElement & ConfigurationElementCollection MethodInfo.Invoke抛出TargetInvocationException C# - MethodInfo.Invoke Throw TargetInvocationException c# 如何使用包含自定义“元素”的嵌套“ConfigurationElementCollection”实现自定义“ConfigurationSection” - How to implement a custom "ConfigurationSection" with a nested "ConfigurationElementCollection" containing a custom "Element" TargetInvocationException? - TargetInvocationException? ConfigurationElementCollection 和 Linq - ConfigurationElementCollection and Linq LINQ的ConfigurationElementCollection - ConfigurationElementCollection LINQ 从ConfigurationPropertyAttribute中获取ConfigurationElementCollection中的GetKey? - GetKey in ConfigurationElementCollection from ConfigurationPropertyAttribute? 具有基本类型的ConfigurationElementCollection - ConfigurationElementCollection with primitive types 如何使用ConfigurationElementCollection实现ConfigurationSection - How to implement a ConfigurationSection with a ConfigurationElementCollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM