Udostępnij za pośrednictwem


Accessing Default Members

Any type can have a default member, which is a member that is invoked when no member name is given. You can invoke default members by calling the Type.InvokeMember method with String.Empty ("") as the member name. InvokeMember retrieves the System.Reflection.DefaultMemberAttribute attribute from the type and then invokes it. The following example invokes the default member of Class1, and the value it returns is assigned to o.

Dim c As New Class1()
Dim o As Object
o = c.GetType().InvokeMember("", BindingFlags.InvokeMethod, Nothing, c, New Object(){})
Console.WriteLine("Default member result: {0}", o)
Class1 c = new Class1();
object o;
o = c.GetType().InvokeMember("", BindingFlags.InvokeMethod, null, c, new object[0]);
Console.WriteLine("Default member result: {0}", o);
Class1^ c = gcnew Class1();
Object^ o;
o = c->GetType()->InvokeMember("", BindingFlags::InvokeMethod, nullptr, c, gcnew array<Object^>(0));
Console::WriteLine("Default member result: {0}", o);

Default members are indicated by the DefaultMemberAttribute attribute on the declaring type. The class shown in the following example has the DefaultMemberAttribute manually added. Do not add the DefaultMemberAttribute manually if the class has an indexer declared; in that case, the compiler automatically adds the attribute.

<DefaultMember("GetIVal")> _
Public Class Class1
    Private ival As Integer
    Private sval As String

    Public Sub New()
        ival = 5050
        sval = "6040"
    End Sub

    Public Function GetIVal() As Integer
        Return ival
    End Function

    Public Function GetSVal() As String
        Return sval
    End Function
End Class
[DefaultMember("GetIVal")]
public class Class1
{
    private int ival;
    private string sval;

    public Class1()
    {
        ival = 5050;
        sval = "6040";
    }

    public int GetIVal()
    {
        return ival;
    }

    public string GetSVal()
    {
        return sval;
    }
}
[DefaultMember("GetIVal")]
public ref class Class1
{
private:
    int ival;
    String^ sval;

public:
    Class1()
    {
        ival = 5050;
        sval = "6040";
    }

    int GetIVal()
    {
        return ival;
    }

    String^ GetSVal()
    {
        return sval;
    }
};

The following example shows how to retrieve the default member by retrieving the custom attribute for the default member.

Dim classType As Type = GetType(Class1)
Dim attribType As Type = GetType(DefaultMemberAttribute)
Dim defMem As DefaultMemberAttribute = _
    CType(Attribute.GetCustomAttribute(CType(classType, MemberInfo), attribType), _
    DefaultMemberAttribute)
Dim memInfo() As MemberInfo = classType.GetMember(defMem.MemberName)
Type classType = typeof(Class1);
Type attribType = typeof(DefaultMemberAttribute);
DefaultMemberAttribute defMem =
    (DefaultMemberAttribute)Attribute.GetCustomAttribute((MemberInfo)classType, attribType);
MemberInfo[] memInfo = classType.GetMember(defMem.MemberName);
Type^ classType = Class1::typeid;
Type^ attribType = DefaultMemberAttribute::typeid;
DefaultMemberAttribute^ defMem =
    (DefaultMemberAttribute^)Attribute::GetCustomAttribute((MemberInfo^)classType, attribType);
array<MemberInfo^>^ memInfo = classType->GetMember(defMem->MemberName);

It might be simpler to use the Type.GetDefaultMembers method, which produces the same result. However, GetDefaultMembers throws an InvalidOperationException if more than one default member is defined on the type. The following example shows the syntax for GetDefaultMembers.

Dim t As Type = GetType(Class1)
Dim memInfo() As MemberInfo = t.GetDefaultMembers()
Type t = typeof(Class1);
MemberInfo[] memInfo = t.GetDefaultMembers();
Type^ t = Class1::typeid;
array<MemberInfo^>^ memInfo = t->GetDefaultMembers();

You can also get the custom attributes for a type and select just the DefaultMemberAttribute, by using the GetCustomAttributes method. The following example demonstrates this technique.

Dim t As Type = GetType(Class1)
Dim customAttribs() As Object _
    = t.GetCustomAttributes(GetType(DefaultMemberAttribute), False)
If customAttribs.Length > 0 Then
    Dim defMem As DefaultMemberAttribute = CType(customAttribs(0), DefaultMemberAttribute)
    Dim memInfo() As MemberInfo = t.GetMember(defMem.MemberName)
    If memInfo.Length > 0 Then
        Console.WriteLine("Default Member: {0}", memInfo(0).Name)
    End If
End If
Type t = typeof(Class1);
object[] customAttribs = t.GetCustomAttributes(typeof(DefaultMemberAttribute), false);
if (customAttribs.Length > 0)
{
    DefaultMemberAttribute defMem = (DefaultMemberAttribute)customAttribs[0];
    MemberInfo[] memInfo = t.GetMember(defMem.MemberName);
    if (memInfo.Length > 0)
    {
        Console.WriteLine("Default Member: {0}", memInfo[0].Name);
    }
}
Type^ t = Class1::typeid;
array<Object^>^ customAttribs = t->GetCustomAttributes(DefaultMemberAttribute::typeid, false);
if (customAttribs->Length > 0)
{
    DefaultMemberAttribute^ defMem = (DefaultMemberAttribute^)customAttribs[0];
    array<MemberInfo^>^ memInfo = t->GetMember(defMem->MemberName);
    if (memInfo->Length > 0)
    {
        Console::WriteLine("Default Member: {0}", memInfo[0]->Name);
    }
}

See Also

Reference

DefaultMemberAttribute

Type.GetDefaultMembers

Concepts

Viewing Type Information