Export (0) Print
Expand All

DefaultMemberAttribute Class

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

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

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

/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface) */ 
public final class DefaultMemberAttribute extends Attribute
SerializableAttribute 
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface) 
public final class DefaultMemberAttribute extends Attribute
Not applicable.

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]

import System.*;
import System.Reflection.*;
import System.IO.*;

/** @attribute DefaultMemberAttribute("Age")
 */
public class MyClass
{
    public void Name(String s)
    {
    } //Name

    /** @property 
     */
    public int get_Age()
    {
        return 20;
    } //get_Age

    public static void main(String[] args)
    {
        try {
            Type myType = MyClass.class.ToType();
            MemberInfo memberInfoArray[] = myType.GetDefaultMembers();
            if (memberInfoArray.length > 0) {
                for (int iCtr = 0; iCtr < memberInfoArray.length; iCtr++) {
                    MemberInfo memberInfoObj = memberInfoArray[iCtr];
                    Console.WriteLine("The default member name is: "
                        + memberInfoObj.ToString());
                }
            }
            else {
                Console.WriteLine("No default members are available.");
            }
        }
        catch (InvalidOperationException e) {
            Console.WriteLine("InvalidOperationException: " + e.get_Message());
        }
        catch (IOException e) {
            Console.WriteLine("IOException: " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: " + e.get_Message());
        }
    } //main
} //MyClass

System.Object
   System.Attribute
    System.Reflection.DefaultMemberAttribute

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft