共用方式為


Type.HasElementType Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a value indicating whether the current Type encompasses or refers to another type; that is, whether the current Type is an array, a pointer, or is passed by reference.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public ReadOnly Property HasElementType As Boolean
public bool HasElementType { get; }

Property Value

Type: System.Boolean
true if the Type is an array, a pointer, or is passed by reference; otherwise, false.

Remarks

For example, Type.GetType("Int32[]").HasElementType returns true, but Type.GetType("Int32").HasElementType returns false. HasElementType also returns true for "Int32*" and "Int32&".

If the current Type represents a generic type, or a type parameter in the definition of a generic type or generic method, this property always returns false.

Examples

The following example returns true or false depending on whether or not the object has an element type. An array type, a ref or out parameter, or a constructed ByRef type all have element types.

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 Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' 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()
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) & vbCrLf

      ' Test an array type without defining an array.
      t = GetType(Example())
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) & vbCrLf

      ' 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()
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) & vbCrLf

      ' 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
      outputBlock.Text += String.Format("HasElementType is '{0}' for ref parameter types.", t.HasElementType) & vbCrLf
      t = parms(1).ParameterType
      outputBlock.Text += String.Format("HasElementType is '{0}' for <Out> parameter types.", t.HasElementType) & vbCrLf

      ' 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()
      outputBlock.Text += String.Format("HasElementType is '{0}' for pointer types.", t.HasElementType) & vbCrLf
      t = GetType(Example).MakeByRefType()
      outputBlock.Text += String.Format("HasElementType is '{0}' for ByRef types.", t.HasElementType) & vbCrLf
   End Sub

End Class

' This example produces the following output:
'
'HasElementType is 'True' for array types.
'HasElementType is 'True' for array types.
'HasElementType is 'True' for array types.
'HasElementType is 'True' for ref parameter types.
'HasElementType is 'True' for <Out> parameter types.
'HasElementType is 'True' for ByRef types.
using System;
using System.Reflection;

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // 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();
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) + "\n";

      // Test an array type without defining an array.
      t = typeof(Example[]);
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) + "\n";

      // 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();
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) + "\n";

      // 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;
      outputBlock.Text += String.Format("HasElementType is '{0}' for ref parameter types.", t.HasElementType) + "\n";
      t = parms[1].ParameterType;
      outputBlock.Text += String.Format("HasElementType is '{0}' for out parameter types.", t.HasElementType) + "\n";

      // 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();
      outputBlock.Text += String.Format("HasElementType is '{0}' for pointer types.", t.HasElementType) + "\n";
      t = typeof(Example).MakeByRefType();
      outputBlock.Text += String.Format("HasElementType is '{0}' for ByRef types.", t.HasElementType) + "\n";
   }
}

/* This example produces the following output:

HasElementType is 'True' for array types.
HasElementType is 'True' for array types.
HasElementType is 'True' for array types.
HasElementType is 'True' for ref parameter types.
HasElementType is 'True' for out parameter types.
HasElementType is 'True' for ByRef types.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.