如何判断一个类是否继承另一个类?

he_hawk 2005-07-26 11:49:05
如何判断一个类是否继承另一个类?

谢谢!
...全文
556 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
linfengcyl 2005-07-26
  • 打赏
  • 举报
回复
下列方法未经测试:

基类:basec,继承类实例:obj
方法一.
return (obj is typeof(basec));
方法二.
return (typeof(basec)).IsInstanceOf(obj);
方法三:
return (Type.Equals(typeof(basec),(typeof(obj)).BaseType);//直接继承,否则要递归
北京的雾霾天 2005-07-26
  • 打赏
  • 举报
回复
可以用is,比如:
if (t is someType)
{
//yes
}
else
{
//no
}
crossrowman 2005-07-26
  • 打赏
  • 举报
回复
写错了 不好意思

if(IsInherit(Page.GetType(),typeof(object)))//判断Page是否从object继承
{
...
}
crossrowman 2005-07-26
  • 打赏
  • 举报
回复
//判断 BaseType 是不是 type 的基类
public bool IsInherit(System.Type type,System.Type BaseType)
{
if(type.BaseType == null) return false;
if(type.BaseType == BaseType) return true;
return IsInherit(type.BaseType ,BaseType);
}
例如:
if(Page.GetType(),typeof(object))//判断Page是否从object继承
{
...
}
okyzx 2005-07-26
  • 打赏
  • 举报
回复
楼上厉害
3tzjq 2005-07-26
  • 打赏
  • 举报
回复
Class1 InheritC;//继承类
Class2 baseC = new Class2;//基类
try
{
Type t =(Class2)InheritC;
if(t != null) return true;
}
catch
{
throw;
}

以上代码未经测试!
he_hawk 2005-07-26
  • 打赏
  • 举报
回复
谢谢各位的帮助!

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧