Export (0) Print
Expand All

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.

System.Object
   System.Attribute
      System.Reflection.DefaultMemberAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Struct _
   Or AttributeTargets.Interface)>
<Serializable>
NotInheritable Public Class DefaultMemberAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct |
   AttributeTargets.Interface)]
[Serializable]
public sealed class DefaultMemberAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Class | AttributeTargets::Struct
   | AttributeTargets::Interface)]
[Serializable]
public __gc __sealed class DefaultMemberAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct |
   AttributeTargets.Interface)
 Serializable
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.

Example

[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 Language Filter in the upper-left corner of the page.

Requirements

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

Show:
© 2014 Microsoft