Type.GetElementType 方法

当在派生类中重写时,返回当前数组、指针或引用类型包含的或引用的对象的 Type

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

语法

声明
Public MustOverride Function GetElementType As Type
用法
Dim instance As Type
Dim returnValue As Type

returnValue = instance.GetElementType
public abstract Type GetElementType ()
public:
virtual Type^ GetElementType () abstract
public abstract Type GetElementType ()
public abstract function GetElementType () : Type

返回值

当前数组、指针或引用类型包含或引用的对象的 Type;如果当前 Type 不是数组或指针,不是按引用传递,或者表示泛型类型或泛型方法的定义中的泛型类型或类型参数,则为 空引用(在 Visual Basic 中为 Nothing)。

备注

对于 Array 类,此方法返回 空引用(在 Visual Basic 中为 Nothing)。

示例

下面的示例说明如何使用 GetElementType 方法。

Imports System

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 'Main
End Class 'TestGetElementType
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());
    }
}
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() );
}
import System.*;

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

这段代码产生以下输出:

System.Int32[] element type is System.Int32
    TestGetElementType element type is null

平台

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

请参见

参考

Type 类
Type 成员
System 命名空间
HasElementType