Object.GetType 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得目前執行個體的 Type。
public:
Type ^ GetType();
public Type GetType ();
member this.GetType : unit -> Type
Public Function GetType () As Type
傳回
目前執行個體的確切執行階段類型。
範例
下列程式碼範例示範如何 GetType 傳回目前實例的執行時間類型。
using namespace System;
public ref class MyBaseClass {};
public ref class MyDerivedClass: 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
*/
using System;
public class MyBaseClass {
}
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());
}
}
// The example displays the following output:
// mybase: Type is MyBaseClass
// myDerived: Type is MyDerivedClass
// object o = myDerived: Type is MyDerivedClass
// MyBaseClass b = myDerived: Type is MyDerivedClass
type MyBaseClass() = class end
type MyDerivedClass() =
inherit MyBaseClass()
let myBase = MyBaseClass()
let myDerived = MyDerivedClass()
let o: obj = myDerived
let b: MyBaseClass = myDerived
printfn $"mybase: Type is {myBase.GetType()}"
printfn $"myDerived: Type is {myDerived.GetType()}"
printfn $"object o = myDerived: Type is {o.GetType()}"
printfn $"MyBaseClass b = myDerived: Type is {b.GetType()}"
// The example displays the following output:
// mybase: Type is MyBaseClass
// myDerived: Type is MyDerivedClass
// object o = myDerived: Type is MyDerivedClass
// MyBaseClass b = myDerived: Type is MyDerivedClass
' Define a base and a derived class.
Public Class MyBaseClass
End Class
Public Class MyDerivedClass : Inherits MyBaseClass
End Class
Public Class Test
Public Shared Sub Main()
Dim base As New MyBaseClass()
Dim derived As New MyDerivedClass()
Dim o As Object = derived
Dim b As MyBaseClass = derived
Console.WriteLine("base.GetType returns {0}", base.GetType())
Console.WriteLine("derived.GetType returns {0}", derived.GetType())
Console.WriteLine("Dim o As Object = derived; o.GetType returns {0}", o.GetType())
Console.WriteLine("Dim b As MyBaseClass = derived; b.Type returns {0}", b.GetType())
End Sub
End Class
' The example displays the following output:
' base.GetType returns MyBaseClass
' derived.GetType returns MyDerivedClass
' Dim o As Object = derived; o.GetType returns MyDerivedClass
' Dim b As MyBaseClass = derived; b.Type returns MyDerivedClass
備註
因為 System.Object 是 .NET 類型系統中所有型別的基類, GetType 所以方法可用來傳回 Type 代表所有 .NET 類型的物件。 .NET 可辨識下列五種類型類別:
衍生自 System.Object 的類別
實值型別,衍生自 System.ValueType 。
從 .NET Framework 2.0 開始衍生 System.Object 的介面。
列舉,衍生自 System.Enum 。
衍生自 System.MulticastDelegate 的委派。
針對兩個 物件 x
,且 y
具有相同的執行時間類型, Object.ReferenceEquals(x.GetType(),y.GetType())
會傳 true
回 。 下列範例使用 GetType 方法搭配 ReferenceEquals 方法,判斷一個數值是否與兩個其他數值相同。
int n1 = 12;
int n2 = 82;
long n3 = 12;
Console.WriteLine("n1 and n2 are the same type: {0}",
Object.ReferenceEquals(n1.GetType(), n2.GetType()));
Console.WriteLine("n1 and n3 are the same type: {0}",
Object.ReferenceEquals(n1.GetType(), n3.GetType()));
// The example displays the following output:
// n1 and n2 are the same type: True
// n1 and n3 are the same type: False
open System
let n1 = 12
let n2 = 82
let n3 = 12L
printfn $"n1 and n2 are the same type: {Object.ReferenceEquals(n1.GetType(), n2.GetType())}"
printfn $"n1 and n3 are the same type: {Object.ReferenceEquals(n1.GetType(), n3.GetType())}"
// The example displays the following output:
// n1 and n2 are the same type: True
// n1 and n3 are the same type: False
Module Example
Public Sub Main()
Dim n1 As Integer = 12
Dim n2 As Integer = 82
Dim n3 As Long = 12
Console.WriteLine("n1 and n2 are the same type: {0}",
Object.ReferenceEquals(n1.GetType(), n2.GetType()))
Console.WriteLine("n1 and n3 are the same type: {0}",
Object.ReferenceEquals(n1.GetType(), n3.GetType()))
End Sub
End Module
' The example displays the following output:
' n1 and n2 are the same type: True
' n1 and n3 are the same type: False
注意
若要判斷物件是否為特定類型,您可以使用語言的類型比較關鍵字或建構。 例如,您可以在 TypeOf…Is
Visual Basic 中使用 建構,或在 is
C# 中使用 關鍵字。
方法 GetType 是由衍生自 Object 的所有型別所繼承。 這表示除了使用您自己的語言比較關鍵字之外,您還可以使用 GetType 方法來判斷特定物件的類型,如下列範例所示。
object[] values = { (int) 12, (long) 10653, (byte) 12, (sbyte) -5,
16.3, "string" };
foreach (var value in values) {
Type t = value.GetType();
if (t.Equals(typeof(byte)))
Console.WriteLine("{0} is an unsigned byte.", value);
else if (t.Equals(typeof(sbyte)))
Console.WriteLine("{0} is a signed byte.", value);
else if (t.Equals(typeof(int)))
Console.WriteLine("{0} is a 32-bit integer.", value);
else if (t.Equals(typeof(long)))
Console.WriteLine("{0} is a 64-bit integer.", value);
else if (t.Equals(typeof(double)))
Console.WriteLine("{0} is a double-precision floating point.",
value);
else
Console.WriteLine("'{0}' is another data type.", value);
}
// The example displays the following output:
// 12 is a 32-bit integer.
// 10653 is a 32-bit integer.
// 12 is an unsigned byte.
// -5 is a signed byte.
// 16.3 is a double-precision floating point.
// 'string' is another data type.
let values: obj[] =
[| 12; 10653L; 12uy
-5y; 16.3; "string" |]
for value in values do
let t = value.GetType()
if t.Equals typeof<byte> then
printfn $"{value} is an unsigned byte."
elif t.Equals typeof<sbyte> then
printfn $"{value} is a signed byte."
elif t.Equals typeof<int> then
printfn $"{value} is a 32-bit integer."
elif t.Equals typeof<int64> then
printfn $"{value} is a 64-bit integer."
elif t.Equals typeof<double> then
printfn $"{value} is a double-precision floating point."
else
printfn $"'{value}' is another data type."
// The example displays the following output:
// 12 is a 32-bit integer.
// 10653 is a 32-bit integer.
// 12 is an unsigned byte.
// -5 is a signed byte.
// 16.3 is a double-precision floating point.
// 'string' is another data type.
Module Example
Public Sub Main()
Dim values() As Object = { 12, CLng(10653), CByte(12),
CSbyte(-5), 16.3, "string" }
For Each value In values
Dim t AS Type = value.GetType()
If t.Equals(GetType(Byte))
Console.WriteLine("{0} is an unsigned byte.", value)
ElseIf t.Equals(GetType(SByte))
Console.WriteLine("{0} is a signed byte.", value)
ElseIf t.Equals(GetType(Integer))
Console.WriteLine("{0} is a 32-bit integer.", value)
ElseIf t.Equals(GetType(Long))
Console.WriteLine("{0} is a 64-bit integer.", value)
ElseIf t.Equals(GetType(Double))
Console.WriteLine("{0} is a double-precision floating point.",
value)
Else
Console.WriteLine("'{0}' is another data type.", value)
End If
Next
End Sub
End Module
' The example displays the following output:
' 12 is a 32-bit integer.
' 10653 is a 32-bit integer.
' 12 is an unsigned byte.
' -5 is a signed byte.
' 16.3 is a double-precision floating point.
' 'string' is another data type.
物件 Type 會公開與目前 Object 類別相關聯的中繼資料。