【问题标题】:C# Get properties from SettingsPropertyCollectionC# 从 SettingsPropertyCollection 获取属性
【发布时间】:2011-07-28 14:02:47
【问题描述】:

我的 web.config 中有配置文件提供程序

    <profile defaultProvider="MyProvider">
      <providers>
.......
      <properties>
        <add name="CustomField1" type="string" />
        <add name="CustomField2" type="string" />
        <add name="CustomField3" type="string" />
        <add name="CustomField4" type="string" />
      </properties>
    </profile>

如何获取包含所有可用属性(CustomField1、CustomField2....)的 string[] 数组

编辑: 找到了可行的解决方案,但不确定它是否是最好和最简单的解决方案。

var allCustomProperties =
                    profile.GetType().GetProperties().Where(l => l.PropertyType.Name == "String" && l.CanWrite == true).Select(
                        l => l.Name).ToArray();

【问题讨论】:

    标签: c# .net asp.net web-config icollection


    【解决方案1】:

    我会同意的:

    string[] props = ProfileBase.Properties.Cast<SettingsProperty>()
                .Select( p => p.Name ).ToArray();
    

    您必须同时导入 System.Web.Profile 和 System.Configuration 命名空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 2018-07-13
      • 2016-01-02
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      相关资源
      最近更新 更多