C# | Type.GetDefaultMembers() Method
Type.GetDefaultMembers() Method is used to find the members defined for the current Type whose DefaultMemberAttribute is set.
Syntax: public virtual System.Reflection.MemberInfo[] GetDefaultMembers ();
Return Value: This method returns an array of MemberInfo objects representing all default members of the current Type or an empty array of type MemberInfo, if the current Type does not have default members.
Below programs illustrate the use of Type.GetDefaultMembers() Method:
Example 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]); } } |
Output:
Result is: Char Chars [Int32]
Example 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" ; } } } |
Output:
Result is: System.String name
Reference: