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“表示字符串哈希值。

相关问题