2011-04-03 27 views
5

性質我有概況提供在我的web.configC#獲取來自SettingsPropertyCollection

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

我怎樣才能得到的String []包含所有avaliable屬性(自定義字段,自定義字段....)

陣列編輯: 找到工作的解決方案,但不知道它是否是最好的和最簡單的。

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

回答

8

我會去與:

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

你必須同時導入System.Web.Profile和System.Configuration命名空間。

+0

你是對的。它正在工作 – 2011-04-03 15:00:21

+0

這是一種享受。謝謝!! – 2013-01-09 05:17:29