다음을 통해 공유


Type.HasElementType 속성

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

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public ReadOnly Property HasElementType As Boolean
‘사용 방법
Dim instance As Type
Dim value As Boolean

value = instance.HasElementType
public bool HasElementType { get; }
public:
virtual property bool HasElementType {
    bool get () sealed;
}
/** @property */
public final boolean get_HasElementType ()
public final function get HasElementType () : boolean

속성 값

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

설명

예를 들어, Type.GetType("Int32[]").HasElementType은 true이지만 Type.GetType("Int32").HasElementType은 false입니다. 또한 HasElementType은 "Int32*" 및 "Int32&"에 대해 true입니다.

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

예제

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

Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices

Public Class Example

    ' This method is for demonstration purposes.
    Public Shared Sub Test(ByRef x As Integer, <Out> ByRef y As Integer)
    End Sub

    Public Shared Sub Main()
        ' All of the following display 'True'.

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

        ' Test an array type without defining an array.
        t = GetType(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 = GetType(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.
        Dim mi As MethodInfo = GetType(Example).GetMethod("Test")
        Dim parms() As ParameterInfo = 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)

        ' 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 = GetType(Example).MakePointerType()
        Console.WriteLine("HasElementType is '{0}' for pointer types.", t.HasElementType)
        t = GetType(Example).MakeByRefType()
        Console.WriteLine("HasElementType is '{0}' for ByRef types.", t.HasElementType)
    End Sub 

End Class
// 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);
    }
}
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;

public ref class Example
{
public:
    // This method is for demonstration purposes. It includes a
    // tracking reference (C# ref, VB ByRef), an out parameter,
    // and a pointer.
    void Test(int% x, [OutAttribute()] int% y, int* z)
    { 
        *z = x = y = 0; 
    }    
};

int main()
{
    // All of the following display 'True'.

    // Define a managed array, get its type, and display HasElementType. 
    array<Example^>^ examples = {gcnew Example(), gcnew Example()};
    Type^ t = examples::typeid; 
    Console::WriteLine(t);
    Console::WriteLine("HasElementType is '{0}' for managed 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 = Example::typeid->MakeArrayType();
    Console::WriteLine("HasElementType is '{0}' for managed 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 = Example::typeid->GetMethod("Test");
    array<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 = Example::typeid->MakePointerType();
    Console::WriteLine("HasElementType is '{0}' for pointer types.", t->HasElementType);
    t = Example::typeid->MakeByRefType();
    Console::WriteLine("HasElementType is '{0}' for ByRef types.", t->HasElementType);
}
import System.*;

public class MyClass
{
} //MyClass

public class Type_HasElementType
{
    public static void main(String[] args)
    {
        try {
            // A nonarray, pointer, or reference element. 
            MyClass myObject1 = new MyClass();
            Object myObject2 = myObject1;

            // Define an array. 
            MyClass myObject3[] = new MyClass[5];
            Object myObject4 = myObject3;
            Object myObjects[] = new Object[] { myObject1, myObject2,
                myObject3, myObject4 };
            Console.WriteLine("\nCheck whether the object is an array, "
                +"a pointer, or a reference type.\n");
            for (int i = 0; i < myObjects.get_Length(); i++) {
                if (myObjects.get_Item(i).GetType().get_HasElementType()) {
                    Console.WriteLine("myObject{0} is an array, pointer, or "
                        +"reference type.", System.Convert.ToString(i));
                }
                else {
                    Console.WriteLine("myObject{0} is not an array, pointer, " 
                        +"or reference type.", System.Convert.ToString(i));
                }
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: {0} \n", e.get_Message());
        }
    } //main
} //Type_HasElementType

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Type 클래스
Type 멤버
System 네임스페이스
HasElementTypeImpl
IsArray
IsPointer
IsByRef
GetElementType
GetType