C# 如何从ConfigurationElementCollection中获取配置属性名称

C# 如何从ConfigurationElementCollection中获取配置属性名称,c#,C#,我目前陷入了如何从ConfigurationElementCollection对象中获取所有配置属性的问题。在这篇文章中,我已经打印出定制配置文件的源代码,以及处理对给定定制配置文件的访问的config类的源代码 configuration> configSections> section name="Section1" type="MyProject.Section1, Section1" /> /configS

我目前陷入了如何从ConfigurationElementCollection对象中获取所有配置属性的问题。在这篇文章中,我已经打印出定制配置文件的源代码,以及处理对给定定制配置文件的访问的config类的源代码

    configuration>
        configSections>
            section name="Section1" type="MyProject.Section1, Section1" />
        /configSections>
        Section1>
        Section1FieldDefinitions>
            add id="1" project="Proj1" browsername="Order1"/>
            add id="2" project="Proj2" browsername="Order2"/>
            add id="3" project="Proj3" browsername="Order3"/>
        /Section1FieldDefinitions>
    /Section1>
    /configuration>
project.config文件的源。我不得不删除xml开括号,因为我无法用开括号发布代码


配置类的源



我目前需要的是一个解决方案,在该解决方案中,我可以迭代所有Section1FieldDefinitions,并从配置属性中获取字段名和字段值。例如,我们有三个配置元素——id、project和browsername。我需要按名称和值列出这三个属性。

我认为您需要使用反射来列出所有属性。 像这样:

var obj = new Section1FieldDefinitionsElement();
foreach(var prop in obj.GetType().GetProperties()) {
    Console.WriteLine("{0}={1} ({2})", prop.Name, prop.GetValue(obj, null), prop.Module.Name);
}

可以提供更多信息。

我认为您希望使用反射列出所有属性。 像这样:

var obj = new Section1FieldDefinitionsElement();
foreach(var prop in obj.GetType().GetProperties()) {
    Console.WriteLine("{0}={1} ({2})", prop.Name, prop.GetValue(obj, null), prop.Module.Name);
}

可能会提供更多信息。

尝试发布一些实际代码。请提供您的代码、您的努力和预期结果。好的。我在将源代码添加到帖子中时遇到了问题。但现在一切都应该准备好了。正如我所写,我需要类Section1FieldDefinitionsElement中的配置属性(id、project和browsername,包括配置文件中的给定值)尝试发布一些实际代码请提供您的代码、您的努力和预期结果。好的。我在将源代码添加到帖子中时遇到了问题。但现在一切都应该准备好了。正如我所写的,我需要类Section1FieldDefinitionsElement中的配置属性(id、project和browsername,包括配置文件中的给定值)