MSDN Library
信息
您所需的主题如下所示。但此主题未包含在此库中。

Type.HasElementType 属性

2013/12/13

获取一个值,通过该值指示当前 Type 是包含还是引用另一类型,即当前 Type 是数组、指针还是通过引用传递。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

public bool HasElementType { get; }

属性值

类型: System.Boolean
如果 Type 为数组、指针或是通过引用传递的,则为 true;否则为 false

例如,Type.GetType("Int32[]").HasElementType 返回 true,而 Type.GetType("Int32").HasElementType 返回 false。对于 "Int32*" 和 "Int32&",HasElementType 也返回 true

如果当前 Type 表示泛型类型,或泛型类型或泛型方法的定义中的类型参数,则此属性始终返回 false

下面的示例返回 truefalse,具体取决于对象是否具有元素类型。数组类型、refout 参数以及构造的 ByRef 类型全部具有元素类型。


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.
 */


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示:
© 2016 Microsoft