7

我正在開發一個需要提供身份驗證以及角色和配置文件管理的C#Web服務。我需要每個配置文件都具有List類型的屬性。在web.config配置文件部分看起來是這樣的:在.NET中使用List類型的配置文件屬性成員

<profile defaultProvider="MyProfileProvider" enabled="true"> 
    <providers> 
    <remove name="MyProfileProvider"/> 
    <add connectionStringName="MySqlServer" 
     applicationName="MyApp" 
     name="MyProfileProvider" 
     type="System.Web.Profile.SqlProfileProvider" /> 
    </providers> 
    <properties> 
    <add name="Websites" type="System.Collections.Generic.List&lt;String&gt;" serializeAs="Binary"/> 
    </properties> 
</profile> 

然而,當我開始在互聯網服務和嘗試訪問它返回以下錯誤性質:

System.Configuration.ConfigurationErrorsException:嘗試加載此屬性的類型導致以下錯誤:無法加載類型'System.Collections.Generic.List <字符串>'。 (C:\ Projects \ MyProject \ web.config 58行)---> System.Web.HttpException:無法加載類型'System.Collections.Generic.List <字符串>'。

有沒有辦法使用泛型集合來達到此目的?

回答

11

經過更多搜索,我終於找到答案。解決方案是使用由其名稱空間限定的類型名稱。這意味着,我用我的問題:

<add name="Websites" type="System.Collections.Generic.List`1[System.String]" serializeAs="Binary"/> 

我還發現,也可以指定在其他程序集中定義的類。要使用這些,您需要裝配限定的名稱。例如,這將是「System.Collections.Generic.HashSet`1 [[System.String,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]],System.Core,Version = 3.5.0.0, Culture = neutral,PublicKeyToken = b77a5c561934e089「表示字符串哈希值。