This documentation is archived and is not being maintained.

DefaultMemberAttribute Class

Defines the member of a type that is the default member used by InvokeMember. The default member is a name given to a type.

For a list of all members of this type, see DefaultMemberAttribute Members.


[Visual Basic]
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Struct _
   Or AttributeTargets.Interface)>
NotInheritable Public Class DefaultMemberAttribute
   Inherits Attribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct |
public sealed class DefaultMemberAttribute : Attribute
[AttributeUsage(AttributeTargets::Class | AttributeTargets::Struct
   | AttributeTargets::Interface)]
public __gc __sealed class DefaultMemberAttribute : public
   AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct |
class DefaultMemberAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


[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
            Return 20
        End Get
    End Property

    Public Shared Sub Main()
            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
                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]


using System;
using System.Reflection;
using System.IO;

public class MyClass
    public void Name(String s) {}
    public int Age
            return 20;
    public static void Main()
            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());
                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);


#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::IO;

public __gc class MyClass {
   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 Language Filter in the upper-left corner of the page.


Namespace: System.Reflection

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

Assembly: Mscorlib (in Mscorlib.dll)

See Also

DefaultMemberAttribute Members | System.Reflection Namespace