ASP.Net和的GetType() [英] ASP.Net and GetType()

查看:246
本文介绍了ASP.Net和的GetType()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望得到一个类型,我创建了的BasePage的对象。每一页对象基于关闭的BasePage。举例来说,我有一个为Login.aspx,在我的code-背后,有一个方法显示的类:

I want to get a type of a "BasePage" object that I am creating. Every Page object is based off BasePage. For instance, I have a Login.aspx and in my code-behind and a class that has a method Display:

Display(BasePage page) {
    ResourceManager manager = new ResourceManager(page.GetType());
}

在我的项目结构我有一个默认的资源文件和一个伪翻译资源文件。如果我设置尝试是这样的:

In my project structure I have a default resource file and a psuedo-translation resource file. If I set try something like this:

Display(BasePage page) {
    ResourceManager manager = new ResourceManager(typeof(Login));
}

返回翻译页面。经过一番研究,我发现,page.GetType()。toString()方法返回的东西ASP_login.aspx的效果怎样才能得到实际的code类背后的类型,这样,我得到类型的对象登录的BasePage那是源于?

it returns the translated page. After some research I found that page.GetType().ToString() returned something to the effect of "ASP_login.aspx" How can I get the actual code behind class type, such that I get an object of type "Login" that is derived from "BasePage"?

在此先感谢!

推荐答案

如果你的code-旁边看起来是这样的:

If your code-beside looks like this:

public partial class _Login : BasePage 
 { /* ... */ 
 }

然后,你将获得与它的键入对象的 typeof运算(_login) 的。要动态获取类型,可以递归地找到它:

Then you would get the Type object for it with typeof(_Login). To get the type dynamically, you can find it recursively:

Type GetCodeBehindType()
 { return getCodeBehindTypeRecursive(this.GetType());
 }

Type getCodeBehindTypeRecursive(Type t)
 { var baseType = t.BaseType;
   if (baseType == typeof(BasePage)) return t;
   else return getCodeBehindTypeRecursive(baseType);
 }

这篇关于ASP.Net和的GetType()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆