System.Type.IsArrayImpl 方法

方法描述

当在派生类中重写时,实现 IsArray 属性并确定 Type 是否为数组。

语法定义(C# System.Type.IsArrayImpl 方法 的用法)

protected abstract bool IsArrayImpl()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Boolean 如果 Type 是数组,则为 true;否则为 false。

提示和注释

Array 类的实例必须返回 false,因为它是对象而不是数组。

System.Type.IsArrayImpl 方法例子

下面的示例重写 MyTypeDelegator 类中的 IsArrayImpl 方法,检查变量是不是数组,并显示结果。

using System;
using System.Reflection;
public class MyTypeDelegator : TypeDelegator
{
    public string myElementType = null;
    public Type myType;
    public MyTypeDelegator(Type myType) : base(myType)
    {
        this.myType = myType;
    }
    // Override IsArrayImpl().
    protected override bool IsArrayImpl()
    {
        // Determine whether the type is an array.
        if(myType.IsArray)
        {
            myElementType = "array";
            return true;
        }
        // Return false if the type is not an array.
        return false;  
    }
}
public class Type_IsArrayImpl
{
    public static void Main()
    {
        try
        {
            int myInt = 0 ; 
            // Create an instance of an array element.
            int[] myArray = new int[5];
            MyTypeDelegator myType = new MyTypeDelegator(myArray.GetType());
            Console.WriteLine("\nDetermine whether the variable is an array.\n");
            // Determine whether myType is an array type.  
            if( myType.IsArray)
                Console.WriteLine("The type of myArray is {0}.", myType.myElementType);
            else
                Console.WriteLine("myArray is not an array.");
            myType = new MyTypeDelegator(myInt.GetType());

            // Determine whether myType is an array type. 
            if( myType.IsArray)
                Console.WriteLine("The type of myInt is {0}.", myType.myElementType);
            else
                Console.WriteLine("myInt is not an array.");
        }
        catch( Exception e )
        {
            Console.WriteLine("Exception: {0}", e.Message );
        }
    }
}

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。