Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DefaultMemberAttribute Class

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

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface)] 
public sealed class DefaultMemberAttribute : Attribute
/** @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.


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);
        }
    }
}

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
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft