C#中的Type.GetDefaultMembers()方法用于搜索为设置了DefaultMemberAttribute的当前Type定义的成员。
public virtual System.Reflection.MemberInfo[] GetDefaultMembers ();
现在让我们看一个实现Type.GetDefaultMembers()方法的示例-
using System; using System.Reflection; [DefaultMemberAttribute("subject")] public class Demo { public static void Main(){ Type t = typeof(Demo); MemberInfo[] member = t.GetDefaultMembers(); if (member.Length != 0){ for (int i = 0; i < member.Length; i++) Console.WriteLine("{0}", member[i]); } else { Console.WriteLine("Default Member isn't there!"); } } public void Name(String str) {} public String subject{ get{ return "Mathematics"; } } }
输出结果
这将产生以下输出-
System.String subject
现在让我们来看另一个实现Type.GetDefaultMembers()方法的示例-
using System; using System.Reflection; [DefaultMemberAttribute("rank")] public class Demo { public static void Main(){ Type t = typeof(Demo); MemberInfo[] member = t.GetDefaultMembers(); if (member.Length != 0){ for (int i = 0; i < member.Length; i++) Console.WriteLine("{0}", member[i]); } else { Console.WriteLine("Default Member isn't there!"); } } public void Name(String str) {} public int rank{ get{ return 5; } } }
输出结果
这将产生以下输出-
Int32 rank