2011-10-12 73 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

相關問題