簡體   English   中英

ProfileCommon - 在運行時轉換失敗

[英]ProfileCommon — casting in run-time fails

摘要:將基類轉換為派生類后返回Null。 但是,在轉換之前,基類對象似乎沒問題。

詳細信息:我正在重寫舊的asp.net WebForms應用程序,以便能夠使用MVC方法擴展它。 作為過程的一部分,我將Web站點項目轉換為Web應用程序項目。 在這種情況下,不會自動生成ProfileCommon類。 因此,我從舊項目中復制了自動生成的類定義,並將其放置為utils\\ProfileCommon.cs 該文件的內容是(簡化為一個也從捷克語等效名稱重命名的屬性):

using System;
using System.Web;
using System.Web.Profile;

namespace proj_app.utils
{
    public class ProfileCommon : System.Web.Profile.ProfileBase
    {
        public virtual string Name {
            get { return ((string)(this.GetPropertyValue("Name"))); }
            set { this.SetPropertyValue("Name", value); }
        }

        public static ProfileCommon GetProfile(string username)
        {
            return ((ProfileCommon)(ProfileBase.Create(username)));
        }
    }
}

不同之處還在於我從GetProfile()方法中刪除了virtual並使其成為static ProfileBase沒有GetProfile()方法;所以它應該沒問題吧?)

代碼編譯得很好。 但是,我可以在瀏覽器中觀察到以下內容(松散翻譯,行號與示例不匹配):

The object of the ProfileCommon type cannot be casted to proj_app.utils.ProfileCommon.
Description: ... unhandled exception during the web request...

Details about the exception: The object of the ProfileCommon type cannot be casted to proj_app.utils.ProfileCommon.

Zdrojová chyba:

Line 36:         public static ProfileCommon GetProfile(string username)
Line 37:         {
Line 38:             return ((ProfileCommon)(ProfileBase.Create(username)));
Line 39:         }
Line 40:     }


Source file: d:\__RizeniProjektu\aplikace\proj_app\utils\ProfileCommon.cs    Line: 38

Stack trace:
[InvalidCastException: Objekt typu ProfileCommon nelze přetypovat na typ proj_app.utils.ProfileCommon.]
   proj_app.utils.ProfileCommon.GetProfile(String username) in d:\__RizeniProjektu\aplikace\proj_app\utils\ProfileCommon.cs:38
   Users_seznam.Page_Load(Object sender, EventArgs e) in d:\__RizeniProjektu\aplikace\proj_app\Users\seznam.aspx.cs:29
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

調試時, ProfileBase.Create(username)返回對象,其中Name屬性的正確值具有預期值(調試器知道如何獲取值)。 但是,在強制轉換后,生成的對象引用為null (我必須承認我在C#方面不是很好;我來自C ++。)我還嘗試拆分命令以獲取ProfileBase p = ProfileBase.Create(username); (該對象不為null),然后才將其轉換為ProfileCommon q = p as ProfileCommon; q為空)。

更新:奇怪。 我已將我的課程重命名為ProfileComm ,清理了項目,從頭開始編譯。 沒有ProfileCommon目錄中的字符串(也沒有在源,而不是在二進制文件中,名為.suo *的* .dll文件,將* .PDB,而不是任何其他文件)。 即使是文件名更名為ProfileComm.cs不具備ProfileCommon在* .csproj的。 GetProfile()的來源已更改為:

public class ProfileComm : System.Web.Profile.ProfileBase
{
    ...
    public static ProfileComm GetProfile(string username)
    {
        object p = ProfileBase.Create(username);
        Type t = p.GetType();
        string s = t.Name;

        return ((ProfileComm)(p));
    }
}

現在瀏覽器中的異常詳細信息讀取(從另一種語言翻譯): System.InvalidCastException: Object of the type ProfileCommon cannot be casted to the type proj_app.utils.ProfileComm.

調試時,調試器顯示p的類型是ProfileCommon

調試器顯示的類型

我沒有從那里類型名稱理念ProfileCommon在下滑。難道是相關做一個版本的.NET框架的? 我該怎么檢查? ProfileCommon類仍然可以以某種方式動態生成嗎?

看來您有兩種類型的ProfileCommon您應該檢查您的來源以確認這一點。 你也能更具體和有ProfileBase.Create(username)返回proj_app.utils.ProfileCommon ,而不是僅僅ProfileCommon

也可以嘗試查看編譯器認為該對象的確是什么,請參閱此帖子: 無法解析類型:我在c#中轉換自定義類時出現問題


看起來你和ASP.NET類沖突,我要么完全創建一個新類,要么創建一個ASP.NET ProfileCommon類的超類。

ASP.NET使用ProfileBase類創建用於用戶配置文件的類。 啟動啟用了用戶配置文件的應用程序后,ASP.NET將創建一個類型為ProfileCommon的新類,它繼承自ProfileBase類。 對於配置文件配置部分中定義的每個屬性,將為ProfileCommon類添加強類型訪問器。 ProfileCommon類的強類型訪問器分別調用ProfileBase基類的GetPropertyValue和SetPropertyValue方法來檢索和設置配置文件屬性值。 將ProfileCommon類的實例設置為ASP.NET應用程序的Profile屬性的值。

https://msdn.microsoft.com/en-us/library/system.web.profile.profilebase(v=vs.110).aspx

ASP.NET自動創建在應用程序啟動時從ProfileBase繼承的ProfileCommon類。 如果要使用自己的配置文件實現,則需要在web.config中明確指定它。 就像是:

<profile inherits="proj_app.utils.ProfileCommon">
....

這樣,ProfileBase.Create將返回指定類型的實例,而不是自動生成的實例。

MSDN了解更多詳情

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM