Type.GetDefaultMembers Method
Searches for the members defined for the current Type whose DefaultMemberAttribute is set.
[Visual Basic] Public Overridable Function GetDefaultMembers() As MemberInfo() [C#] public virtual MemberInfo[] GetDefaultMembers(); [C++] public: virtual MemberInfo* GetDefaultMembers() []; [JScript] public function GetDefaultMembers() : MemberInfo[];
Return Value
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.
Remarks
This method can be overridden by a derived class.
Members include properties, methods, fields, events, and so on.
The following table shows what members of a base class are returned by the Get methods when reflecting on a type.
| Member Type | Static | Non-Static |
|---|---|---|
| Constructor | No | No |
| Field | No | Yes. A field is always hide-by-name-and-signature. |
| Event | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
| Method | No | Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
| Nested Type | No | No |
| Property | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
- Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.
- For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
- Custom attributes are not part of the common type system.
Example
[Visual Basic, C#, C++] The following example obtains the default member information of MyClass and displays the default members.
[Visual Basic] Imports System Imports System.Reflection Imports System.IO Imports Microsoft.VisualBasic <DefaultMemberAttribute("Age")> Public Class [MyClass] Public Sub Name(ByVal s As String) End Sub 'Name Public ReadOnly Property Age() As Integer Get Return 20 End Get End Property Public Shared Sub Main() Try Dim myType As Type = GetType([MyClass]) Dim memberInfoArray As MemberInfo() = myType.GetDefaultMembers() If memberInfoArray.Length > 0 Then Dim memberInfoObj As MemberInfo For Each memberInfoObj In memberInfoArray Console.WriteLine("The default member name is: " + memberInfoObj.ToString()) Next memberInfoObj Else Console.WriteLine("No default members are available.") End If Catch e As InvalidOperationException Console.WriteLine("InvalidOperationException: " + e.Message) Catch e As IOException Console.WriteLine("IOException: " + e.Message) Catch e As Exception Console.WriteLine("Exception: " + e.Message) End Try End Sub 'Main End Class '[MyClass] [C#] using System; using System.Reflection; using System.IO; [DefaultMemberAttribute("Age")] public class MyClass { public void Name(String s) {} public int Age { get { return 20; } } public static void Main() { try { Type myType = typeof(MyClass); MemberInfo[] memberInfoArray = myType.GetDefaultMembers(); if (memberInfoArray.Length > 0) { foreach(MemberInfo memberInfoObj in memberInfoArray) { Console.WriteLine("The default member name is: " + memberInfoObj.ToString()); } } else { Console.WriteLine("No default members are available."); } } catch(InvalidOperationException e) { Console.WriteLine("InvalidOperationException: " + e.Message); } catch(IOException e) { Console.WriteLine("IOException: " + e.Message); } catch(Exception e) { Console.WriteLine("Exception: " + e.Message); } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Reflection; using namespace System::IO; [DefaultMemberAttribute(S"Age")] public __gc class MyClass { public: void Name(String* s) {} __property int get_Age() { return 20; } }; int main() { try { Type* myType = __typeof(MyClass); MemberInfo* memberInfoArray[] = myType->GetDefaultMembers(); if (memberInfoArray->Length > 0) { System::Collections::IEnumerator* myEnum = memberInfoArray->GetEnumerator(); while (myEnum->MoveNext()) { MemberInfo* memberInfoObj = __try_cast<MemberInfo*>(myEnum->Current); Console::WriteLine(S"The default member name is: {0}", memberInfoObj); } } else { Console::WriteLine(S"No default members are available."); } } catch (InvalidOperationException* e) { Console::WriteLine(S"InvalidOperationException: {0}", e->Message); } catch (IOException* e) { Console::WriteLine(S"IOException: {0}", e->Message); } catch (Exception* e) { Console::WriteLine(S"Exception: {0}", e->Message); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
Type Class | Type Members | System Namespace | MemberInfo | DefaultMemberAttribute | GetMember | GetMembers | FindMembers