Lire en anglais

Partager via


Type.HasElementType Propriété

Définition

Obtient une valeur indiquant si le Type actuel englobe ou se réfère à un autre type, c'est-à-dire si le Type actuel est un tableau ou un pointeur ou encore s'il est passé par référence.

C#
public bool HasElementType { get; }

Valeur de propriété

true si Type est un tableau ou un pointeur ou encore s'il est passé par référence ; sinon, false.

Implémente

Exemples

L’exemple suivant retourne true ou false selon que l’objet est un tableau, un type référence ou un pointeur.

C#
// This code must be compiled with the /unsafe switch:
//   csc /unsafe source.cs
using System;
using System.Reflection;

public class Example
{
    // This method is for demonstration purposes.
    unsafe public void Test(ref int x, out int y, int* z)
    {
        *z = x = y = 0;
    }

    public static void Main()
    {
        // All of the following display 'True'.

        // Define an array, get its type, and display HasElementType.
        int[] nums = {1, 1, 2, 3, 5, 8, 13};
        Type t = nums.GetType();
        Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType);

        // Test an array type without defining an array.
        t = typeof(Example[]);
        Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType);

        // When you use Reflection Emit to emit dynamic methods and
        // assemblies, you can create array types using MakeArrayType.
        // The following creates the type 'array of Example'.
        t = typeof(Example).MakeArrayType();
        Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType);

        // When you reflect over methods, HasElementType is true for
        // ref, out, and pointer parameter types. The following
        // gets the Test method, defined above, and examines its
        // parameters.
        MethodInfo mi = typeof(Example).GetMethod("Test");
        ParameterInfo[] parms = mi.GetParameters();
        t = parms[0].ParameterType;
        Console.WriteLine("HasElementType is '{0}' for ref parameter types.", t.HasElementType);
        t = parms[1].ParameterType;
        Console.WriteLine("HasElementType is '{0}' for out parameter types.", t.HasElementType);
        t = parms[2].ParameterType;
        Console.WriteLine("HasElementType is '{0}' for pointer parameter types.", t.HasElementType);

        // When you use Reflection Emit to emit dynamic methods and
        // assemblies, you can create pointer and ByRef types to use
        // when you define method parameters.
        t = typeof(Example).MakePointerType();
        Console.WriteLine("HasElementType is '{0}' for pointer types.", t.HasElementType);
        t = typeof(Example).MakeByRefType();
        Console.WriteLine("HasElementType is '{0}' for ByRef types.", t.HasElementType);
    }
}

Remarques

Par exemple, Type.GetType(« Int32[] »). HasElementType retourne true, mais Type.GetType(« Int32 »). HasElementType retourne false. HasElementType retourne true également pour « Int32* » et « Int32& ».

Si le actuel Type représente un type générique ou un paramètre de type dans la définition d’un type générique ou d’une méthode générique, cette propriété retourne falsetoujours .

S’applique à

Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Voir aussi