Object.GetType 方法

.NET Framework 2.0
其他版本
 
  • .NET Framework 4.5
  • .NET Framework 4
  • .NET Framework 3.5
  • .NET Framework 3.0
  • Silverlight
10(共 12)对本文的评价是有帮助 - 评价此主题

获取当前实例的 Type。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)

语法
C#
C++
VB
复制
public:
Type^ GetType ()
J#
复制
public Type GetType ()
JScript
复制
public function GetType () : Type

返回值

Type 实例,表示当前实例的确切运行时类型。

备注

对于具有相同运行时类型的两个对象 xy,Object.ReferenceEquals(x.GetType(),y.GetType()) 返回 true

Type 对象公开与当前 Object 的类关联的元数据。

示例

下面的代码示例说明 GetType 返回当前实例的运行时类型。

C#
C++
复制
using namespace System;
public ref class MyBaseClass: public Object{};

public ref class MyDerivedClass: public MyBaseClass{};

int main()
{
   MyBaseClass^ myBase = gcnew MyBaseClass;
   MyDerivedClass^ myDerived = gcnew 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 

*/

J#
复制
import System.*;

public class MyBaseClass extends Object
{
} //MyBaseClass

public class MyDerivedClass extends MyBaseClass
{
} //MyDerivedClass

public class Test
{
    public static void main(String[] args)
    {
        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());
    } //main
} //Test

/*

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 

*/

JScript
复制
import System

public class MyBaseClass extends Object {
}

public class MyDerivedClass extends MyBaseClass {
}

public class Test {

   public static function Main() {
      var myBase : MyBaseClass = new MyBaseClass();
      var myDerived : MyDerivedClass = new MyDerivedClass();
      var o = myDerived;
      var b : MyBaseClass = 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());
   }
}

Test.Main();

/*

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 

*/

平台

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
 
参考出处:
http://msdn.microsoft.com/zh-cn/library/system.object.gettype(v=vs.80).aspx#Y579

你可能感兴趣的:(windows,.net,object,function,Class,silverlight)