C# 是否可以在ASP.NET C中使用继承#
C# 是否可以在ASP.NET C中使用继承#,c#,asp.net,oop,inheritance,c#-4.0,C#,Asp.net,Oop,Inheritance,C# 4.0,我有一个有解决方案和类库的项目 我创建了一个名为ManagerClass.cs的基类,该基类继承自System.Web.UI.Page以使该类成为我的所有基类,在类库中,我有一个名为AlertMessage.cs的类,我想直接从任何继承的类中使用该类作为示例: 这是从Manager类继承的默认类: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We
我有一个有解决方案和类库的项目 我创建了一个名为ManagerClass.cs的基类,该基类继承自System.Web.UI.Page以使该类成为我的所有基类,在类库中,我有一个名为AlertMessage.cs的类,我想直接从任何继承的类中使用该类作为示例: 这是从Manager类继承的默认类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomModalMessages
{
public partial class Default : ManagerClass
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPopUp_Click(object sender, EventArgs e)
{
AlertMessage.Show("", false, Page, GetType());
}
}
}
此外,我的Manager类如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SharedComponent;
namespace CustomModalMessages
{
public class ManagerClass : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
AlertMessage.Show("", true, Page, GetType());
base.OnLoad(e);
}
protected override void OnError(EventArgs args)
{
Exception ex = Server.GetLastError().GetBaseException();
string sStackTrace = ex.StackTrace.ToString();
string sErrorDate = DateTime.UtcNow.ToString();
string sErrorPage = ex.Source.ToString();
string sErrorMessage = ex.Message.ToString();
}
}
}
如您所见,我使用SharedComponent制作了一个;我希望在所有继承的类中公开这个using,而不调用using
我的层次结构如下所示:
提前感谢
使用
语句不能在C#中继承。它们甚至不编译为代码-它们只是开发人员的一个实用工具,因此他不必键入完全限定的类型名(例如SharedComponent.AlertMessage
)
使用,您可以看到该类:
using System;
public class C {
public void M() {
Console.WriteLine("");
}
}
编译为
.class public auto ansi beforefieldinit C
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig
instance void M () cil managed
{
// Method begins at RVA 0x2050
// Code size 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr ""
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret
} // end of method C::M
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x205e
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method C::.ctor
} // end of class C
在方法
M
中带有IL_0006
的行是此处的关注点。IL代码中指定了一个完全限定的类型名称,并且找不到using
语句。using
语句不能在C#中继承。它们甚至不编译为代码-它们只是开发人员的一个实用工具,因此他不必键入完全限定的类型名(例如SharedComponent.AlertMessage
)
使用,您可以看到该类:
using System;
public class C {
public void M() {
Console.WriteLine("");
}
}
编译为
.class public auto ansi beforefieldinit C
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig
instance void M () cil managed
{
// Method begins at RVA 0x2050
// Code size 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr ""
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret
} // end of method C::M
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x205e
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method C::.ctor
} // end of class C
在方法
M
中带有IL_0006
的行是此处的关注点。IL代码中指定了一个完全限定的类型名,并且找不到using
语句。No这是不可能的。。。只有在aspx页面中才有可能。。。通过在web.config中指定。。可以为所有.aspx页面包含程序集命名空间。。但是,如果您不想使用SharedComponent包含,则您没有用于aspx.cs页面的功能代码>,您需要键入SharedComponent.AlertMessage.Show(…)代码>。你不能完全避免它隐藏在代码中。我可能会将这个类移到我当前的项目中,而不是类库中。这是不可能的。。。只有在aspx页面中才有可能。。。通过在web.config中指定。。可以为所有.aspx页面包含程序集命名空间。。但是,如果您不想使用SharedComponent包含,则您没有用于aspx.cs页面的功能代码>,您需要键入SharedComponent.AlertMessage.Show(…)代码>。你无法在代码背后完全避免它。我可能会将该类移动到我当前的项目中,而不是类库。如果我没有弄错,你的答案是:没有办法继承使用。如果我没有弄错,你的答案是:没有办法继承使用。