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

解决方案 »

  1.   


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

  2.   

    class A{
     void test();
    }
    class B:A{
      void test();
    }
      

  3.   

    var m = typeof(B).GetMethod("test");
    var isDeclaredInB = m.DeclaringType == typeof(B);
      

  4.   

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

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

  5.   

    public static bool IsMethodDeclaredInType(Type type, string method)
    {
        var m = type.GetMethod(method);
        return m==null? false : m.DeclaringType == type;
    }