Type.GetElementType()方法用于在派生类中重写时,返回当前数组,指针或引用类型所包含或引用的对象的Type。
Syntax: public abstract Type GetElementType ();
Return Value: This method returns the Type of the object encompassed or referred to by the current array, pointer, or reference type, or null if the current Type is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter in the definition of a generic type or generic method.
下面的程序说明了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