當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。