的确是可以通过new一个新的GetType函数来使得派生类调用此方法.
class MyTestGetType
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
MyTestGetType instance = new MyTestGetType();
Console.WriteLine(instance.GetType());
}
public new Type GetType()
{
return typeof(System.String);
}
}
来个例子,一切便知,来也!
using System;
using System.Reflection;
using System.Security;
public class MyClass1
{
public MyClass1(int i){}
public static void Main()
{
try
{
Type myType = typeof(MyClass1);
Type[] types = new Type[1];
types[0] = typeof(int);
// Get the public instance constructor that takes an integer parameter.
ConstructorInfo constructorInfoObj = myType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public, null,
CallingConventions.HasThis, types, null);
if(constructorInfoObj != null)
{
Console.WriteLine("The constructor of MyClass1 that is a public " +
"instance method and takes an integer as a parameter is: ");
Console.WriteLine(constructorInfoObj.ToString());
}
else
{
Console.WriteLine("The constructor of MyClass1 that is a public instance " +
"method and takes an integer as a parameter is not available.");
}
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: " + e.Message);
}
catch(ArgumentException e)
{
Console.WriteLine("ArgumentException: " + e.Message);
}
catch(SecurityException e)
{
Console.WriteLine("SecurityException: " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public __gc class MyClass1 {
public:
MyClass1(int i) {}
}
;
int main() {
try {
Type* myType = __typeof(MyClass1);
Type* types[] = new Type*[1];
types->Item[0] = __typeof(int);
// Get the public instance constructor that takes an integer parameter.
ConstructorInfo* constructorInfoObj = myType->GetConstructor(static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), 0,
CallingConventions::HasThis, types, 0);
if (constructorInfoObj != 0) {
Console::WriteLine(S"The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: ");
Console::WriteLine(constructorInfoObj);
} else {
Console::WriteLine(S"The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available.");
}
} catch (ArgumentNullException* e) {
Console::WriteLine(S"ArgumentNullException: {0}", e->Message);
} catch (ArgumentException* e) {
Console::WriteLine(S"ArgumentException: {0}", e->Message);
} catch (SecurityException* e) {
Console::WriteLine(S"SecurityException: {0}", e->Message);
} catch (Exception* e) {
Console::WriteLine(S"Exception: {0}", e->Message);
}
}
[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。
public static void Main() {
MyBaseClass myBase = new MyBaseClass();
MyDerivedClass myDerived = new MyDerivedClass();
object o = myDerived;
MyBaseClass b = myDerived;
Console.WriteLine("mybase: Type is {0}", myBase.GetType());
Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
}
}
/*
This code produces the following output.
mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass