自定义 ConfigurationElementCollection 抛出

我正在尝试ConfigurationSection使用ConfigurationElementCollection. 但是当我使用它时会抛出一个ConfigurationErrorsException带有 2 个详细信息的异常TargetInvocationException并且MissingMethodException


我在构造函数中安慰一些消息,结果只是“新的 AssembySettingSection”。在 AssembySettingSection base["settings"] 处抛出异常。


应用配置


<?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>

部分代码(需要时发布其他人)


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

        }

    }


}


慕虎7371278
浏览 165回答 1
1回答

12345678_0001

抱歉,我在问题中使用了错误的元素类型。无论如何,为其他用户回答这个问题。问题已解决。public class AssembySettingSection : ConfigurationSection{&nbsp; &nbsp; public AssembySettingSection()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine("new AssembySettingSection");&nbsp; &nbsp; }&nbsp; &nbsp; [ConfigurationProperty("settings", IsDefaultCollection = false)]&nbsp; &nbsp; [ConfigurationCollection(typeof(AssembySettingElement))]//Changed AssembySettingCollection to AssembySettingElement&nbsp; &nbsp; public AssembySettingCollection Settings&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return base["settings"] as AssembySettingCollection;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP