Type.HasElementType Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.Booleantrue if the Type is an array, a pointer, or is passed by reference; otherwise, false.
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.
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.