Type.GetElementType 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当在派生类中重写时,返回当前数组、指针或引用类型包含的或引用的对象的 Type。
public:
abstract Type ^ GetElementType();
public abstract Type GetElementType ();
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.
*/
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 返回 。