📜  C#| Type.IsArrayImpl()方法

📅  最后修改于: 2021-05-29 15:16:42             🧑  作者: Mango

Type.IsArrayImpl()方法在派生类中重写时使用,实现IsArray属性并确定Type是否为数组。

下面的程序说明了Type.IsArrayImpl()方法的用法:

范例1:

// C# program to demonstrate the
// Type.IsArrayImpl() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating and initializing object of MyClass
        MyClass mytype = new MyClass(typeof(int));
  
        // checking if mytype has any
        // elementtype or not
        if (mytype.HasElementType)
            Console.WriteLine("The type of myArray is {0}.",
                                        mytype.elementtype);
        else
            Console.WriteLine("myArray is not an array, pointer,"+
                                           "or reference type.");
    }
}
  
// Defining MyClass extended from TypeDelegator
public class MyClass : TypeDelegator {
  
    // creating and initializing
    // elementtype with null
    public string elementtype = null;
  
    // creating and initializing 
    // type with null
    private Type type = null;
  
    // Constructor
    public MyClass(Type type)
                : base(type)
    {
        this.type = type;
    }
  
    // Override Type.IsArrayImpl().
    protected override bool IsArrayImpl()
    {
        // Determine whether the type is an array.
        if (type.IsArray) 
        {
            elementtype = "array";
            return true;
        }
        // Return false if the type is not
        // a reference, array, or pointer type.
        return false;
    }
}
输出:
myArray is not an array, pointer,or reference type.

范例2:

// C# program to demonstrate the
// Type.IsArrayImpl() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating and initializing object of MyClass
        MyClass mytype = new MyClass(typeof(int[,,,, ]));
  
        // checking if mytype has
        // any elementtype or not
        if (mytype.HasElementType)
            Console.WriteLine("The type of {0} is array.",
                                             mytype.type);
        else
            Console.WriteLine("myArray is not an array,"+
                          "pointer, or reference type.");
    }
}
  
// Defining MyClass extended from TypeDelegator
public class MyClass : TypeDelegator {
  
    // creating and initializing
    // elementtype with null
    public string elementtype = null;
  
    // creating and initializing 
    // type with null
    public Type type = null;
  
    // Constructor
    public MyClass(Type type)
                 : base(type)
    {
        this.type = type;
    }
  
    // Override Type.IsArrayImpl().
    protected override bool IsArrayImpl()
    {
  
        // Determine whether the
        // type is an array.
        if (type.IsArray)
        {
            elementtype = "array";
            return true;
        }
  
        // Return false if the type is not
        // a reference, array, or pointer type.
        return false;
    }
}
输出:
The type of System.Int32[,,,,] is array.

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.isarrayimpl?view=netframework-4.8