C# | Type.GetDefaultMembers() Method
Last Updated :
19 Dec, 2019
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:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
object obj = "Ram" ;
Type type = obj.GetType();
MemberInfo[] info = type.GetDefaultMembers();
for ( int i = 0; i < info.Length; i++)
Console.WriteLine( "Result is: {0}" , info[i]);
}
}
|
Output:
Result is: Char Chars [Int32]
Example 2:
using System;
using System.Globalization;
using System.Reflection;
[DefaultMemberAttribute( "name" )] class GFG {
public static void Main()
{
Type type = typeof (GFG);
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" );
}
}
public void Name(String s) {}
public String name
{
get
{
return "Ram" ;
}
}
}
|
Output:
Result is: System.String name
Reference:
Share your thoughts in the comments
Please Login to comment...