2011-10-12 76 views
3

我有一个Web应用程序项目并在web.config中实现了配置文件属性。当我发现我无法访问ProfileCommon对象我用Google搜索,发现了一些解决方法: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监视窗口中执行的行,它成功了!该事件显示ProfileBase.Create(userCurrent.UserName)的类型为ProfileCommon而没有演员。智能感知不起作用,但可以检查该对象,并且我看到它具有定义的两个配置文件属性。

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

回答

1

ProfileCommon在编译时不可用,因为它是应用程序启动时创建的类。这是由于您可以在web.config中的profile元素中定义的属性,因此可以动态添加它们。 请参阅official documentation中的备注。

+0

这是一个奇怪的行为,但是当我通过实现我的自定义类“KIP_Profile:ProfileBase”修复该问题时,出现错误“此配置文件属性已被定义。”当试图运行语句'创建(userCurrent.UserName)' – Mentoliptus

+1

如果您已经在类和web.config配置文件部分中定义了相同的属性,就会发生这种情况。 – loodakrawa

+0

谢谢!从'web.config'文件中删除属性定义并在'KIP_Profile'中移动它们解决了这个问题! – Mentoliptus

相关问题