Type.GetElementType 方法
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在衍生類別中覆寫時,傳回由目前陣列、指標或參考類型所包含或參考物件的 Type。
public:
abstract Type ^ GetElementType();
C#
public abstract Type GetElementType ();
C#
public abstract Type? GetElementType ();
abstract member GetElementType : unit -> Type
Public MustOverride Function GetElementType () As Type
目前的陣列、指標或參考類型所內含或參考之物件的 Type,如果目前的 null
不是陣列或指標、未以傳址方式傳遞,或表示泛型類型或泛型類型或泛型方法定義中之類型參數,則為 Type。
下列範例將示範如何使用 GetElementType
方法。
using namespace System;
public ref class TestGetElementType{};
int main()
{
array<Int32>^array = {1,2,3};
Type^ t = array->GetType();
Type^ t2 = t->GetElementType();
Console::WriteLine( "The element type of {0} is {1}.", array, t2 );
TestGetElementType^ newMe = gcnew TestGetElementType;
t = newMe->GetType();
t2 = t->GetElementType();
Console::WriteLine( "The element type of {0} is {1}.", newMe, t2 == nullptr ? "null" : t2->ToString() );
}
/* This code produces the following output:
The element type of System.Int32[] is System.Int32.
The element type of TestGetElementType is null.
*/
C#
using System;
class TestGetElementType
{
public static void Main()
{
int[] array = {1,2,3};
Type t = array.GetType();
Type t2 = t.GetElementType();
Console.WriteLine("The element type of {0} is {1}.",array, t2.ToString());
TestGetElementType newMe = new TestGetElementType();
t = newMe.GetType();
t2 = t.GetElementType();
Console.WriteLine("The element type of {0} is {1}.", newMe, t2==null? "null" : t2.ToString());
}
}
/* This code produces the following output:
The element type of System.Int32[] is System.Int32.
The element type of TestGetElementType is null.
*/
Class TestGetElementType
Public Shared Sub Main()
Dim array As Integer() = {1, 2, 3}
Dim t As Type = array.GetType()
Dim t2 As Type = t.GetElementType()
Console.WriteLine("The element type of {0} is {1}.", array, t2.ToString())
Dim newMe As New TestGetElementType()
t = newMe.GetType()
t2 = t.GetElementType()
If t2 Is Nothing Then
Console.WriteLine("The element type of {0} is {1}.", newMe, "null")
Else
Console.WriteLine("The element type of {0} is {1}.", newMe, t2.ToString())
End If
End Sub
End Class
' This code produces the following output:
'
'The element type of System.Int32[] is System.Int32.
'The element type of TestGetElementType is null.
這個方法會 null
針對 Array 類別傳回。
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |