|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Accessing Default Members
Any type can have a default member, which is a member that is invoked when no member name is given. The following example invokes the default member of Class1, and the value it returns is assigned to i.
Default members are marked with the System.Reflection.DefaultMemberAttribute. The following example shows how to retrieve the default member by retrieving the custom attribute for the default member.
Type t = typeof(DefaultMemberAttribute); DefaultMemberAttribute defMem = (DefaultMemberAttribute)Attribute.GetCustomAttribute(Assembly.GetAssembly(t), t); MemberInfo memInfo = t.GetMember(defMem.MemberName);
It might be simpler to use Type.GetDefaultMembers, which yields exactly the same result. However, GetDefaultMembers throws an InvalidOperationException if there is more than one default member defined on the type. The following code example shows the syntax for GetDefaultMembers.