开发者

C# Get properties from SettingsPropertyCollection

开发者 https://www.devze.com 2023-02-21 12:24 出处:网络
I have profile provider in my web.config <profile defaultProvider=\"MyProvider\"> <providers>

I have profile provider in my 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>

How can I get string[] array containing all avaliable properties (CustomField1, CustomField2....)

Edit: Found working solution but not sure if it's the best and easie开发者_如何学Pythonst one.

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


I'd go with that:

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

You have to import both System.Web.Profile and System.Configuration namespaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消