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


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


当在派生类中重写时,使用Type.HasElementTypeImpl()方法,实现HasElementType属性,并确定当前Type是否包含或引用其他类型。这意味着此方法检查当前Type是数组,指针还是通过引用传递。

用法: protected abstract bool HasElementTypeImpl ();

返回值:如果Type是数组,指针或通过引用传递,则此方法返回true,否则返回false。


以下示例程序旨在说明Type.HasElementTypeImpl()方法的使用:

示例1:

// C# program to demonstrate the 
// Type.HasElementTypeImpl() 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.HasElementTypeImpl(). 
    protected override bool HasElementTypeImpl() 
    { 
  
        // Determine whether the type is an array. 
        if (type.IsArray) { 
            elementtype = "array"; 
            return true; 
        } 
  
        // Determine whether the type is a reference. 
        if (type.IsByRef)  
        { 
            elementtype = "reference"; 
            return true; 
        } 
  
        // Determine whether the type is a pointer. 
        if (type.IsPointer) 
        { 
            elementtype = "pointer"; 
            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.HasElementTypeImpl() 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 {1} is {0}.", 
                       mytype.elementtype, mytype.type); 
        else
            Console.WriteLine("{0} is not an array, pointer, or reference type.", 
                                                                    mytype.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.HasElementTypeImpl(). 
    protected override bool HasElementTypeImpl() 
    { 
        // Determine whether the type is an array. 
        if (type.IsArray)  
        { 
  
            elementtype = "array"; 
            return true; 
        } 
  
        // Determine whether the type is a reference. 
        if (type.IsByRef)  
        { 
            elementtype = "reference"; 
            return true; 
        } 
  
        // Determine whether the type is a pointer. 
        if (type.IsPointer)  
        { 
            elementtype = "pointer"; 
            return true; 
        } 
  
        // Return false if the type is not a  
        // reference, array, or a pointer type 
        return false; 
    } 
}
输出:
The type of System.Int32[,,,,,,] is array.

参考:



相关用法


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