Прочитать на английском

Поделиться через


Type.HasElementTypeImpl Метод

Определение

При переопределении в производном классе реализует свойство HasElementType и определяет, что содержится в текущем объекте Type: непосредственно другой тип или же указывающая на него ссылка (иными словами, является ли текущий объект Type массивом, указателем или параметром или же он передается по ссылке).

protected abstract bool HasElementTypeImpl();

Возвращаемое значение

Значение true, если объект Type является массивом, указателем или параметром, переданным по ссылке; в противном случае — значение false.

Примеры

В следующем примере определяется класс MyTypeDelegator, который переопределяет HasElementTypeImpl метод . Класс main проверяет HasElementType наличие свойства и отображает тип элемента.

using System;
using System.Reflection;
public class MyTypeDelegator : TypeDelegator
{
    public string myElementType = null;
    private Type myType = null ;
    public MyTypeDelegator(Type myType) : base(myType)
    {
        this.myType = myType;
    }
    // Override Type.HasElementTypeImpl().
    protected override bool HasElementTypeImpl()
    {
        // Determine whether the type is an array.
        if(myType.IsArray)
        {
            myElementType = "array";
            return true;
        }
        // Determine whether the type is a reference.
        if(myType.IsByRef)
        {
            myElementType = "reference";
            return true;
        }
        // Determine whether the type is a pointer.
        if(myType.IsPointer)
        {
            myElementType = "pointer";
            return true;
        }
        // Return false if the type is not a reference, array, or pointer type.
        return false;
    }
}
public class Type_HasElementTypeImpl
{
    public static void Main()
    {
        try
        {
            int myInt = 0 ;
            int[] myArray = new int[5];
            MyTypeDelegator myType = new MyTypeDelegator(myArray.GetType());
            // Determine whether myType is an array, pointer, reference type.
            Console.WriteLine("\nDetermine whether a variable is an array, pointer, or reference type.\n");
            if( myType.HasElementType)
                Console.WriteLine("The type of myArray is {0}.", myType.myElementType);
            else
                Console.WriteLine("myArray is not an array, pointer, or reference type.");
            myType = new MyTypeDelegator(myInt.GetType());
            // Determine whether myType is an array, pointer, reference type.
            if( myType.HasElementType)
                Console.WriteLine("The type of myInt is {0}.", myType.myElementType);
            else
                Console.WriteLine("myInt is not an array, pointer, or reference type.");
        }
        catch( Exception e )
        {
            Console.WriteLine("Exception: {0}", e.Message);
        }
    }
}

Комментарии

Например, Type.GetType("Int32[]"). HasElementTypeImpl возвращает true, но Type.GetType("Int32"). HasElementTypeImpl возвращает false. HasElementTypeImpl также возвращает значения true для "Int32*" и "Int32&".

Применяется к

Продукт Версии
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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, 4.8.1
.NET Standard 2.0, 2.1

См. также раздел