用反射创建一个对象,怎么知道某方法是此对象的方法还是基类的方法?

chen6191 2011-10-24 08:09:08
用反射创建一个对象,怎么知道某方法是此对象的类中定义的方法还是其基类中已有的方法?
...全文
71 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jshi123 2011-10-24
  • 打赏
  • 举报
回复
public static bool IsMethodDeclaredInType(Type type, string method)
{
var m = type.GetMethod(method);
return m==null? false : m.DeclaringType == type;
}
真相重于对错 2011-10-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jshi123 的回复:]

var m = typeof(B).GetMethod("test");
var isDeclaredInB = m.DeclaringType == typeof(B);
[/Quote]
class C{
TEST()

}

class B:C
{
TEST();
}

class etc:B
{}
.
.
.
class A:......
{
TEST()

}
再说你怎么知道他的父类的具体名称

。。。
jshi123 2011-10-24
  • 打赏
  • 举报
回复
var m = typeof(B).GetMethod("test");
var isDeclaredInB = m.DeclaringType == typeof(B);
真相重于对错 2011-10-24
  • 打赏
  • 举报
回复
上面怎么判断?/
真相重于对错 2011-10-24
  • 打赏
  • 举报
回复
class A{
void test();
}
class B:A{
void test();
}
快溜 2011-10-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 juliohuang 的回复:]
C# code

(methodInfo.Attributes & MethodAttributes.Virtual) == MethodAttributes.Virtual
//判定是否为虚函数

(methodInfo.Attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.VtableL……
[/Quote]+
黄亮 2011-10-24
  • 打赏
  • 举报
回复

(methodInfo.Attributes & MethodAttributes.Virtual) == MethodAttributes.Virtual
//判定是否为虚函数

(methodInfo.Attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.VtableLayoutMask
//判断是否override
  • 打赏
  • 举报
回复
看看方法原型有木有override

110,538

社区成员

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

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

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