加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.Net和GetType()

发布时间:2020-12-15 08:33:35 所属栏目:百科 来源:网络整理
导读:我想获得一种我正在创建的“BasePage”对象.每个Page对象都基于BasePage.例如,我有一个Login.aspx和我的代码隐藏以及一个具有方法Display的类: Display(BasePage page) { ResourceManager manager = new ResourceManager(page.GetType());} 在我的项目结构
我想获得一种我正在创建的“BasePage”对象.每个Page对象都基于BasePage.例如,我有一个Login.aspx和我的代码隐藏以及一个具有方法Display的类:
Display(BasePage page) {
    ResourceManager manager = new ResourceManager(page.GetType());
}

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

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

它返回翻译的页面.经过一些研究后,我发现page.GetType().ToString()返回了“ASP_login.aspx”的效果.如何获取类类型后面的实际代码,这样我就得到一个类型为“Login”的对象,即派生自“BasePage”?

提前致谢!

解决方法

如果您的代码旁边看起来像这样:
public partial class _Login : BasePage 
 { /* ... */ 
 }

然后你将获得typeof(_Login)的Type对象.要动态获取类型,您可以递归地找到它:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读