pep*_*epr 12 c# asp.net casting visual-studio-2013
摘要:将基类转换为派生类后返回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)));
}
}
}
Run Code Online (Sandbox Code Playgroud)
所不同的也就是我已删除了virtual从GetProfile()方法,并使其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
Run Code Online (Sandbox Code Playgroud)
在调试时,ProfileBase.Create(username)返回对象,其中Name属性的正确值具有期望值(调试器知道如何获取值).但是,在强制转换后,生成的对象引用为null.(我必须承认我在C#方面不是很好;我来自C++.)我也尝试拆分命令来获取ProfileBase p = ProfileBase.Create(username);(对象不为空),然后才将其转换为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));
}
}
Run Code Online (Sandbox Code Playgroud)
现在浏览器中的异常细节读取(从另一种语言翻译): 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.ProfileCommonProfileCommon
也可以尝试查看编译器认为该对象的确是什么,请参阅此帖子:无法解析类型:我在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">
....
Run Code Online (Sandbox Code Playgroud)
这样 ProfileBase.Create 将返回指定类型的实例,而不是自动生成的实例。
| 归档时间: |
|
| 查看次数: |
713 次 |
| 最近记录: |