当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Type.IsArrayImpl()用法及代码示例


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

用法: protected abstract bool IsArrayImpl ();

返回值:如果Type是数组,则此方法返回true,否则返回false。


以下示例程序旨在说明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.

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Type.IsArrayImpl() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。