MethodInfo.GetBaseDefinition 方法
当在派生类中被重写时,为直接或间接的基类(用该实例表示的方法首先在此类中声明)上的方法返回 MethodInfo 对象。
**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public MustOverride Function GetBaseDefinition As MethodInfo
用法
Dim instance As MethodInfo
Dim returnValue As MethodInfo
returnValue = instance.GetBaseDefinition
public abstract MethodInfo GetBaseDefinition ()
public:
virtual MethodInfo^ GetBaseDefinition () abstract
public abstract MethodInfo GetBaseDefinition ()
public abstract function GetBaseDefinition () : MethodInfo
返回值
表示此方法第一个实现的 MethodInfo 对象。
备注
GetBaseDefinition 返回类层次结构中指定方法的第一个定义。
如果此方法是在接口上声明的,则 GetBaseDefinition 将返回此方法。
如果此方法是在基类中声明的,则 GetBaseDefinition 将以如下方式运行:
如果给定方法重写基类中的虚拟定义,则返回该虚拟定义。
如果给定方法是用 new 关键字(同 类型成员 中描述的 newslot 一样)指定的,则返回该给定方法。
如果此方法不是在调用 GetBaseDefinition 的对象类型中定义的,则返回类结构层次中最高一级的方法定义。
若要获取 GetBaseDefinition 方法,请先获取 Type 类。从 Type 获取 MethodInfo。从 MethodInfo 获取 GetBaseDefinition。
示例
此代码示例说明 GetBaseDefinition 的行为。
Imports System
Imports System.Reflection
Public Class GetBaseDef
Public Shared Sub Main()
Dim t As Type = GetType(B)
Dim m As MethodInfo
' Print A Void B().
m = t.GetMethod("B")
Console.WriteLine(m.GetBaseDefinition().DeclaringType.ToString() + " " _
+ m.GetBaseDefinition().ToString())
' Print A Void C().
m = t.GetMethod("C")
Console.WriteLine(m.GetBaseDefinition().DeclaringType.ToString() + " " _
+ m.GetBaseDefinition().ToString())
' Print B Void D().
m = t.GetMethod("D", BindingFlags.Public Or BindingFlags.Instance _
Or BindingFlags.DeclaredOnly)
Console.WriteLine(m.GetBaseDefinition().DeclaringType.ToString() + " " _
+ m.GetBaseDefinition().ToString())
End Sub
End Class
Public Class A
Public Overridable Sub B()
Console.WriteLine("C")
End Sub
Public Overridable Sub C()
Console.WriteLine("C")
End Sub
Public Overridable Sub D()
Console.WriteLine("E")
End Sub
End Class
Public Class B
Inherits A
Public Overrides Sub C()
Console.WriteLine("C")
End Sub
Public Shadows Sub D()
Console.WriteLine("D")
End Sub
End Class
using System;
using System.Reflection;
public class GetBaseDef {
public static void Main(String[] args)
{
Type t = typeof(B);
MethodInfo m;
// Print A Void B().
m = t.GetMethod("B");
Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());
// Print A Void C().
m = t.GetMethod("C");
Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());
// Print B Void D().
m = t.GetMethod("D", (BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.DeclaredOnly));
Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());
}
}
public class A
{
public virtual void B() { Console.WriteLine("C"); }
public virtual void C() { Console.WriteLine("C"); }
public virtual void D() { Console.WriteLine("E"); }
}
public class B : A
{
public override void C() { Console.WriteLine("C"); }
public new void D() { Console.WriteLine("D"); }
}
using namespace System;
using namespace System::Reflection;
public ref class A
{
public:
virtual void B()
{
Console::WriteLine( "C" );
}
virtual void C()
{
Console::WriteLine( "C" );
}
virtual void D()
{
Console::WriteLine( "E" );
}
};
public ref class B: public A
{
public:
virtual void C() override
{
Console::WriteLine( "C" );
}
virtual void D() override
{
Console::WriteLine( "D" );
}
};
int main()
{
Type^ t = B::typeid;
MethodInfo^ m;
// Print A Void B().
m = t->GetMethod( "B" );
Console::WriteLine( "{0} {1}", m->GetBaseDefinition()->DeclaringType, m->GetBaseDefinition() );
// Print A Void C().
m = t->GetMethod( "C" );
Console::WriteLine( "{0} {1}", m->GetBaseDefinition()->DeclaringType, m->GetBaseDefinition() );
// Print B Void D().
m = t->GetMethod( "D", static_cast<BindingFlags>((BindingFlags::Public | BindingFlags::Instance | BindingFlags::DeclaredOnly)) );
Console::WriteLine( "{0} {1}", m->GetBaseDefinition()->DeclaringType, m->GetBaseDefinition() );
}
import System.*;
import System.Reflection.*;
public class GetBaseDef
{
public static void main(String[] args)
{
Type t = B.class.ToType();
MethodInfo m;
// Print A Void B().
m = t.GetMethod("B");
Console.WriteLine((m.GetBaseDefinition().get_DeclaringType() + " "
+ m.GetBaseDefinition()));
// Print A Void C().
m = t.GetMethod("C");
Console.WriteLine((m.GetBaseDefinition().get_DeclaringType() + " "
+ m.GetBaseDefinition()));
// Print B Void D().
m = t.GetMethod("D",BindingFlags.Public | BindingFlags.Instance
| BindingFlags.DeclaredOnly);
Console.WriteLine((m.GetBaseDefinition().get_DeclaringType() + " "
+ m.GetBaseDefinition()));
} //main
} //GetBaseDef
public class A
{
public void B()
{
Console.WriteLine("C");
} //B
public void C()
{
Console.WriteLine("C");
} //C
public void D()
{
Console.WriteLine("E");
} //D
} //A
public class B extends A
{
public void C()
{
Console.WriteLine("C");
} //C
public void D()
{
Console.WriteLine("D");
} //D
} //B
import System;
import System.Reflection;
public class GetBaseDef {
public static function Main() : void
{
var t : Type = B;
var m : MethodInfo;
// Print A Void B().
m = t.GetMethod("B");
Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());
// Print A Void C().
m = t.GetMethod("C");
Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());
// Print B Void D().
m = t.GetMethod("D", (BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.DeclaredOnly));
Console.WriteLine(m.GetBaseDefinition().DeclaringType + " " + m.GetBaseDefinition());
}
}
GetBaseDef.Main();
public class A
{
public function B() : void { Console.WriteLine("C"); }
public function C() : void { Console.WriteLine("C"); }
public function D() : void { Console.WriteLine("E"); }
}
public class B extends A
{
public override function C() : void { Console.WriteLine("C"); }
public hide function D() : void { Console.WriteLine("D"); }
}
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
MethodInfo 类
MethodInfo 成员
System.Reflection 命名空间