DefaultMember Attribute


DefaultMember Attribute

Class

System.Reflection.DefaultMemberAttribute

Applies To

Class, Struct, or Interface

Constructor

     New(memberName) 


memberName (required; String)

The name of the default member

Properties


MemberName (String)

Read-only. The name of the default member. Its value is set by the constructor's memberName parameter.

Description

The <DefaultMember> attribute defines the default member of a structure, class, or interface. The default member is the member executed by the Type object's InvokeMember method when a null string is supplied as the method's name argument.

The Visual Basic Default keyword (part of the Property statement syntax) is ultimately translated by the Visual Basic compiler into the <DefaultMember> attribute. Visual Basic, however, requires that default members be parameterized. The use of the Default keyword allows you to specify a particular array element without having to explicitly reference the member name. For instance, if the Items property is the default member of CampSite, the statement:

     CampSite.Items(10) = "Sleeping bag" 

is functionally identical to:

     CampSite(10) = "Sleeping bag" 

Because the <DefaultMember> attribute, unlike the Default keyword, does not have to refer to a parameterized property, you can use the <DefaultMember> attribute to define default members that are not parameterized. However, this does not allow you to omit a reference to that member in code when using instances of the object. For instance, if the default member of the CashRegister object is a member named TotalValue, you cannot reference it implicitly, as follows:

     CashRegister = 10 

You can, however, invoke that member using the Type.InvokeMember method without explicitly naming it.

The <DefaultMember> attribute and Default keyword are different in one other important respect. If you use <DefaultMember> rather than Default to define a parameterized property as the default member of a class, at runtime, Visual Basic will be unable to resolve implicit references to the member. The sole capability that the <DefaultMember> attribute affords you is the ability to explicitly invoke a default member using the Type.InvokeMember method.

If you use both the Default keyword and the <DefaultMember> attribute in the same class definition, even if both reference the same member, an error occurs.

If memberName is not a member of the class, structure, or interface, the <DefaultMember> attribute is ignored, and no error is raised.


Example

     Option Strict     Imports System     Imports System.Reflection     <DefaultMember("GetName")> _     Public Class Contact        Private contactName As String        Private contactCity As String        Private contactComments(  ) As String        Public Sub New(  )           Me.New("John Doe", "Anywhere, U.S.A.")        End Sub        Public Sub New(ByVal fullName As String, _              ByVal homeCity As String)           MyBase.New(  )           contactName = fullName           contactCity = homeCity        End Sub        Public Property Name(  ) As String           Get              Return contactName           End Get           Set(ByVal value As String)              contactName = value           End Set        End Property        Public Property Comments(index As Integer) As String           Get              Return contactComments(index)           End Get           Set(ByVal value As Integer)              contactComments(index) = value           End Set        End Property        Public Function GetName(  ) As String           Return contactName        End Function        Public Function GetCity(  ) As String           Return contactCity        End Function     End Class     Module GeneralCode        Public Sub Main           Dim useContact As New Contact           Dim contactType As Type = GetType(Contact)           Dim bindings As BindingFlags = BindingFlags.Instance Or _              BindingFlags.Public Or BindingFlags.InvokeMethod           ' ----- The first two lines will produce the same result.           Console.WriteLine(contactType.InvokeMember("", bindings, _              Nothing, useContact, Nothing))           Console.WriteLine(contactType.InvokeMember("GetName", _              bindings, Nothing, useContact, Nothing))           Console.WriteLine(contactType.InvokeMember("GetCity", _              bindings, Nothing, useContact, Nothing))        End Sub     End Module 




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net