3

我有一个 Web 应用程序项目并在 web.config 中实现了配置文件属性。当我发现我无法访问该ProfileCommon对象时,我用谷歌搜索并找到了一些解决方法:

等等。但是没有人说我ProfileCommon在运行时有一个对象。这是我的例子:

<profile enabled="true">
  <properties>
    <add name="AdminRemark"/>
    <add name="FacilityID" type="System.Int64" defaultValue="1"/>
  </properties>
</profile>

此行编译良好且工作正常,但我有硬编码的属性名称:

rcbFacilityID.SelectedValue = 
    (ProfileBase.Create(userCurrent.UserName) as ProfileBase)
    .GetPropertyValue("FacilityID").ToString();

但是这一行没有编译并给出错误:找不到类型或命名空间名称“ProfileCommon”(您是否缺少 using 指令或程序集引用?)):

rcbFacilityID.SelectedValue = 
    (ProfileBase.Create(userCurrent.UserName) as ProfileCommon)
    .FacilityID.ToString();

但是在运行时,我尝试在 Visual Studio Watch 窗口中执行该行并且它起作用了!它事件显示了没有演员表的ProfileBase.Create(userCurrent.UserName)as类型。ProfileCommonIntellisense 不起作用,但可以检查对象,我看到它定义了两个配置文件属性。

如果这是除自定义类之外的唯一方法,我不介意使用硬编码的配置文件属性名称ProfileCommon,但我想解释一下为什么ProfileCommon该类在运行时可用而不是编译时可用?

4

1 回答 1

1

ProfileCommon 在编译时不可用,因为它是在应用程序启动时创建的类。这是由于您可以在 web.config 的配置文件元素内定义的属性,因此可以动态添加它们。见官方文档中的备注。

于 2011-10-17T08:41:31.530 回答