[英]ProfileCommon in WAP working at run-time but not at compile-time
[英]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将返回指定类型的实例,而不是自动生成的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.