📜  C#| Type.GetElementType()方法

📅  最后修改于: 2021-05-30 00:53:18             🧑  作者: Mango

Type.GetElementType()方法用于在派生类中重写时,返回当前数组,指针或引用类型所包含或引用的对象的Type。

下面的程序说明了Type.GetElementType()方法的用法:

范例1:

// C# program to demonstrate the
// Type.GetElementType() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing type
        Type type = typeof(int[,, ]);
  
        // using GetElementType() Method
        Type t = type.GetElementType();
  
        // Display the ElementType
        Console.WriteLine("ElementType is: {0}", t);
    }
}
输出:
ElementType is: System.Int32

范例2:

// C# program to demonstrate the
// Type.GetElementType() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Creating the object of class
        GFG obj = new GFG();
  
        Type typ1 = obj.GetType();
        Type typ2 = typ1.GetElementType();
  
        Console.WriteLine("Element type of {0} is {1}", obj, 
                      typ2==null? "null" : typ2.ToString());
    }
  
}
输出:
Element type of GFG is null

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getelementtype?view=netframework-4.8