영어로 읽기

다음을 통해 공유


Type.HasElementType 속성

정의

현재 Type이 다른 형식을 포함하거나 참조하는지 여부, 즉 현재 Type이 배열 또는 포인터이거나 참조로 전달되는지를 나타내는 값을 가져옵니다.

public bool HasElementType { get; }

속성 값

true이 배열 또는 포인터이거나 참조로 전달되면 Type이고, 그렇지 않으면 false입니다.

구현

예제

다음 예제에서는 개체가 배열, 참조 형식 또는 포인터인지 여부에 따라 또는 를 반환 truefalse 합니다.

// 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);
    }
}

설명

예를 들어 Type.GetType("Int32[]")입니다. HasElementType은 를 반환하지만 Type.GetType("Int32")을 반환 true합니다. HasElementType은 를 반환합니다 false. HasElementType은 "Int32*" 및 "Int32&"에 대해서도 반환 true 합니다.

현재 Type 제네릭 형식 또는 제네릭 형식 또는 제네릭 메서드의 정의에 있는 형식 매개 변수를 나타내는 경우 이 속성은 항상 를 반환합니다 false.

적용 대상

추가 정보