Type.GetProperties()方法用於獲取當前Type的屬性。此方法的重載列表中有2種方法,如下所示:
- GetProperties()方法
- GetProperties(BindingFlags)方法
GetProperties() Method
此方法用於返回當前Type的所有公共屬性。
用法: public System.Reflection.PropertyInfo[] GetProperties ();
返回值:此方法返回代表當前Type的所有公共屬性的PropertyInfo對象的數組,或者如果當前Type沒有公共屬性,則返回一個PropertyInfo類型的空數組。
以下示例程序旨在說明Type.GetProperties()方法的使用:
示例1:
// C# program to demonstrate the 
// Type.GetProperties() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
// Defining class Empty 
public class Empty { } 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(string); 
  
        // try-catch block for handling Exception 
        try { 
  
            // using GetProperties() Method 
            PropertyInfo[] type = objType.GetProperties(); 
  
            // Display the Result 
            Console.WriteLine("Properties of current type is: "); 
            for (int i = 0; i < type.Length; i++) 
                Console.WriteLine(" {0}", type[i]); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e) 
        { 
            Console.Write("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Properties of current type is: Int32 Length Char Chars [Int32]
示例2:
// C# program to demonstrate the 
// Type.GetProperties() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
// Defining class Empty 
public class Empty {} 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(System.Type); 
  
        // try-catch block for handling Exception 
        try { 
  
            // using GetProperties() Method 
            PropertyInfo[] type = objType.GetProperties(); 
  
            // Display the Result 
            Console.WriteLine("Properties of current type is: "); 
            for (int i = 0; i < 10; i++) 
                Console.WriteLine(" {0}", type[i]); 
        } 
  
        // catch ArgumentNullException here 
        catch (ArgumentNullException e)  
        { 
            Console.Write("name is null."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
Properties of current type is: System.Reflection.MemberTypes MemberType System.Type DeclaringType System.Reflection.MethodBase DeclaringMethod System.Type ReflectedType System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute System.Guid GUID System.Reflection.Binder DefaultBinder System.Reflection.Module Module System.Reflection.Assembly Assembly System.RuntimeTypeHandle TypeHandle
GetProperties(BindingFlags) Method
此方法用於在派生類中重寫時使用指定的綁定約束來搜索當前Type的屬性。
用法: public abstract System.Reflection.PropertyInfo[] GetProperties (System.Reflection.BindingFlags bindingAttr);
Here, it takes a bitmask comprised of one or more BindingFlags that specify how the search is conducted or Zero, to return null.
返回值:如果當前Type不具有屬性,或者如果沒有屬性與綁定約束相匹配,則此方法返回一個PropertyInfo對象數組,該對象表示與指定綁定約束匹配的當前Type的所有屬性,或者返回一個PropertyInfo類型的空數組。
示例1:
// C# program to demonstrate the 
// Type.GetProperties(BindingFlags) 
// Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(Dog); 
  
        // using GetProperties() Method 
        PropertyInfo[] type = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public); 
  
        // Display the Result 
        Console.WriteLine("Properties of current type is: "); 
        for (int i = 0; i < type.Length; i++) 
            Console.WriteLine(" {0}", type[i]); 
    } 
} 
  
// Defining class Dog 
public class Dog { 
  
    private string color, breed, type; 
  
    // Constructor 
    public Dog(string color, string breed, string type) 
    { 
        this.color = color; 
        this.breed = breed; 
        this.type = type; 
    } 
  
    // Color Property 
    public String Color 
    { 
        get { return color; } 
    } 
  
    // Breed Property 
    public String Breed 
    { 
        get { return breed; } 
    } 
  
    // Type Property 
    public String Type 
    { 
        get { return type; } 
    } 
  
    // BloodGroup Property 
    private String BloodGroup 
    { 
        get { return "AB+"; } 
    } 
}
輸出:
Properties of current type is: System.String Color System.String Breed System.String Type
示例2:
// C# program to demonstrate the 
// Type.GetProperties(BindingFlags) 
// Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Declaring and initializing object of Type 
        Type objType = typeof(Dog); 
  
        // using GetProperties() Method 
        PropertyInfo[] type = objType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic); 
  
        // Display the Result 
        Console.WriteLine("Properties of current type is: "); 
        for (int i = 0; i < type.Length; i++) 
            Console.WriteLine(" {0}", type[i]); 
    } 
} 
  
// Defining class Dog 
public class Dog { 
  
    private string color, breed, type, bp; 
  
    // Constructor 
    public Dog(string color, string breed, 
                   string type, string bp) 
    { 
        this.color = color; 
        this.breed = breed; 
        this.type = type; 
        this.bp = bp; 
    } 
  
    // Color Property 
    public String Color 
    { 
        get { return color; } 
    } 
  
    // Breed Property 
    public String Breed 
    { 
        get { return breed; } 
    } 
  
    // Type Property 
    public String Type 
    { 
        get { return type; } 
    } 
  
    // BloodGroup Property 
    protected String BloodGroup 
    { 
        get { return "AB+"; } 
    } 
  
    // BloodPressure Property 
    protected String BloodPressure 
    { 
        get { return bp; } 
    } 
}
輸出:
Properties of current type is: System.String BloodGroup System.String BloodPressure
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetProperties() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
