Int32 intI
Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void MyMeth()
Void Main()
System.Type GetType()
Void .ctor()
示例
// cs_operator_typeof2.cs
// Using GetType method
using System;
class GetTypeTest
{
public static void Main()
{
int radius = 3;
Console.WriteLine("Area = {0}", radius*radius*Math.PI);
Console.WriteLine("The type is {0}",
(radius*radius*Math.PI).GetType());
}
}
输出
Area = 28.2743338823081
The type is System.Double
返回值
Type 实例,表示当前实例的确切运行时类型。
备注
对于具有相同运行时类型的两个对象 x 和 y,Object.ReferenceEquals(x.GetType(),y.GetType()) 返回 true。
Type 对象公开与当前 Object 的类关联的元数据。
示例
[C#, JScript] 下列代码示例说明 GetType 返回当前实例的运行时类型。
[C#]
using System;
public class MyBaseClass: Object {
}
public class MyDerivedClass: MyBaseClass {
}
public class Test {
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