Type.GetDefaultMembers()方法用於查找為設置了DefaultMemberAttribute的當前Type定義的成員。
用法: public virtual System.Reflection.MemberInfo[] GetDefaultMembers ();
返回值:如果當前Type沒有默認成員,則此方法返回代表當前Type的所有默認成員的MemberInfo對象的數組,或者返回MemberInfo類型的空數組。
以下示例程序旨在說明Type.GetDefaultMembers()方法的使用:
示例1:
// C# program to demonstrate the
// Type.GetDefaultMembers() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing obj
object obj = "Ram";
// Getting the type of obj
// using GetType() Method
Type type = obj.GetType();
// Getting the DefaultMembers
// using GetDefaultMembers() Method
MemberInfo[] info = type.GetDefaultMembers();
// Display the result
for (int i = 0; i < info.Length; i++)
Console.WriteLine("Result is: {0}", info[i]);
}
}
輸出:
Result is: Char Chars [Int32]
示例2:
// C# program to demonstrate the
// Type.GetDefaultMembers() Method
using System;
using System.Globalization;
using System.Reflection;
// Setting DefaultMemberAttribute
[DefaultMemberAttribute("name")] class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing
// object of Type dataType
Type type = typeof(GFG);
// Getting the DefaultMembers
// using GetDefaultMembers() Method
MemberInfo[] info = type.GetDefaultMembers();
if (info.Length != 0)
{
for (int i = 0; i < info.Length; i++)
Console.WriteLine("Result is: {0}", info[i]);
}
else {
Console.WriteLine("DefaultMember is not found");
}
}
// Defining Member Attributes
public void Name(String s) {}
// Defining property
public String name
{
// property or indexer must
// have at least one accessor
get
{
return "Ram";
}
}
}
輸出:
Result is: System.String name
參考:
相關用法
- 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.GetDefaultMembers() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。