简体   繁体   English

C#-从基类获取页面的BaseType.Name和命名空间

[英]C# - get BaseType.Name and Namespace of the Page from base class

I've a base class for my all pages: 我的所有页面都有一个基类:

//BASE CLASS
public class WebBasePage : Page
{
    protected override void OnLoad(EventArgs e)
    {
          base.OnLoad(e);
          string pageBaseTypeName = ????
          string pageBaseTypeNameMespace = ????
    }
}
//PAGE
public partial class T350112A : WebBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Some code.
    }
}

I want the OnLoad event of this base class to dynamically get the BaseType.Name and the Namespace of the Page that called the base class. 我希望该基类的OnLoad事件动态获取BaseType.Name和调用基类的Page的命名空间。

Is It possible? 可能吗?

I'm assuming you want to get the derived class name and namespace, instead of the base page name and namespace (since that will never change). 我假设您要获取派生的类名称和名称空间,而不是基页名称和名称空间(因为它永远不会改变)。 You would simply get a call to GetType to get this information: 您只需调用GetType即可获取以下信息:

  public override void OnLoad(EventArgs e)
  {
       Type derivedType = GetType();
       string typeName = derivedType.Name;
       string namespace = derivedType.Namespace;
  }

The GetType call will get the information for the running type. GetType调用将获取有关运行类型的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM