Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DefaultMemberAttribute Class

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

System.Object
  System.Attribute
    System.Reflection.DefaultMemberAttribute

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<SerializableAttribute> _
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Interface)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class DefaultMemberAttribute _
	Inherits Attribute

The DefaultMemberAttribute type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryDefaultMemberAttributeInitializes a new instance of the DefaultMemberAttribute class.
Top

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryMemberNameGets the name from the attribute.
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

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.

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]

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library

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