Skip to main content
.NET Framework Class Library
DefaultMemberAttribute Class

Defines the member of a type that is the default member used by InvokeMember.

Inheritance Hierarchy
SystemObject
   SystemAttribute
    System.ReflectionDefaultMemberAttribute

Namespace:   System.Reflection
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_0_0_0%]> _
<[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_0_0_1%]([%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_0_0_2%].Class Or [%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_0_0_3%].Struct Or [%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_0_0_4%].Interface)> _
<[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_0_0_5%](True)> _
Public NotInheritable Class DefaultMemberAttribute _
	Inherits [%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_0_0_6%]
[[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_1_0_0%]]
[[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_1_0_1%]([%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_1_0_2%].Class|[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_1_0_3%].Struct|[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_1_0_4%].Interface)]
[[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_1_0_5%](true)]
public sealed class DefaultMemberAttribute : [%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_1_0_6%]
[[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_2_0_0%]]
[[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_2_0_1%]([%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_2_0_2%]::Class|[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_2_0_3%]::Struct|[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_2_0_4%]::Interface)]
[[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_2_0_5%](true)]
public ref class DefaultMemberAttribute sealed : public [%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_2_0_6%]
[<[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_3_0_0%]>]
[<[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_3_0_1%]>]
[<[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_3_0_2%]([%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_3_0_3%].Class|[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_3_0_4%].Struct|[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_3_0_5%].Interface)>]
[<[%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_3_0_6%](true)>]
type DefaultMemberAttribute =  
    class 
        inherit [%$TOPIC/2dz8e6d8_en-us_VS_110_3_0_3_0_7%] 
    end

The DefaultMemberAttribute type exposes the following members.

Constructors
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps DefaultMemberAttributeInitializes a new instance of the DefaultMemberAttribute class.
Top
Properties
  NameDescription
Public property Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps MemberNameGets the name from the attribute.
Public property TypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top
Methods
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps EqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public method Supported by the XNA Framework MatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method _AttributeGetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeInvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top
Remarks

A property is imported as an indexer (default indexed property in Visual Basic) if the property has arguments and if the name of the property or one of its accessors matches the name specified by the DefaultMemberAttribute. If the DefaultMemberAttribute is not present on the containing type, then the type does not have an indexer. The C# compiler emits the DefaultMemberAttribute on any type containing an indexer. In C# it is an error to manually attribute a type with the DefaultMemberAttribute if the type also declares an indexer.

Examples

The following example uses the DefaultMemberAttribute attribute to make the Age property the default member of the MyClass class.

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]
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);
        }
    }
}
using namespace System;
using namespace System::Reflection;
using namespace System::IO;

[DefaultMemberAttribute("Age")]
public ref class MyClass
{
public:
   void Name( String^ s ){}


   property int Age 
   {
      int get()
      {
         return 20;
      }

   }

};

int main()
{
   try
   {
      Type^ myType = MyClass::typeid;
      array<MemberInfo^>^memberInfoArray = myType->GetDefaultMembers();
      if ( memberInfoArray->Length > 0 )
      {
         System::Collections::IEnumerator^ myEnum = memberInfoArray->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            MemberInfo^ memberInfoObj = safe_cast<MemberInfo^>(myEnum->Current);
            Console::WriteLine( "The default member name is: {0}", memberInfoObj );
         }
      }
      else
      {
         Console::WriteLine( "No default members are available." );
      }
   }
   catch ( InvalidOperationException^ e ) 
   {
      Console::WriteLine( "InvalidOperationException: {0}", e->Message );
   }
   catch ( IOException^ e ) 
   {
      Console::WriteLine( "IOException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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.