DefaultMember Attribute

   
DefaultMember Attribute

Class

System.Reflection.DefaultMemberAttribute

Applies to

Class, Struct, or Interface

Description

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 .NET Default keyword is ultimately translated by the Visual Basic .NET compiler into the <DefaultMember> attribute. Visual Basic, however, requires that default members be parameterized. The use of the default member then allows you to specify a particular array element without having to explicitly reference the member. For instance, if the Items property is the default member of NewObject1, the statement

 NewObject1.Items(10) = "Sleeping bag" 

is functionally identical to

 NewObject(10) = "Sleeping bag" 

This works in VB.NET because the latter code statement is translated by the compiler into a call to the InvokeMember method that looks something like the following:

 Dim t As Type = GetType(NewClass1) Dim iFlags As BindingFlags = BindingFlags.Public Or _                              BindingFlags.Instance Or _                              BindingFlags.SetProperty Dim arr(  ) As Object = { 10, "Sleeping bag" } t.InvokeMember("", iFlags, Nothing, NewObject, arr) 

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. For instance, if the default member of the oCounter object is a member named Value, you cannot reference it implicitly as follows :

 oCounter = 10 

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

The <DefaultMember> attribute and Default keyword are incompatible 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. Hence, the sole capability that the <DefaultMember> attribute affords you is the ability to explicitly invoke a default member using the InvokeMember method of the Type class.

Note that if you use both the Default keyword and the <DefaultMember> attribute in the same class definition, even if both reference the same member, an ExecutionEngineException exception results.

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

Constructor

 New(   memberName   ) 
memberName (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.

Example

 Option Strict Imports System Imports System.Reflection <DefaultMember("GetName")> Public Class CContact Private sName As String Private sCity As String Private sComments(  ) As String Public Sub New(  )    Me.New("John Doe", "Anywhere, U.S.A.") End Sub Public Sub New(strName As String, strCity As String)    MyBase.New(  )    sName = strName    sCity = strCity End Sub Public Property Name As String    Get       Return sName    End Get    Set       sName = Value    End Set End Property Public Property Comments(index As Integer) As String    Get       Return sComments(index)    End Get    Set       sComments(index) = Value    End Set End Property Public Function GetName(  ) As String    Return sName End Function Public Function GetCity(  ) As String    Return sCity End Function End Class Module modMain    Public Sub Main       Dim oContact As New CContact       Dim t As Type = GetType(CContact)       Dim iFlags As BindingFlags = BindingFlags.Instance Or _                                    BindingFlags.Public Or _                                    BindingFlags.InvokeMethod                                Console.WriteLine(t.InvokeMember("", iFlags, Nothing, oContact, _                         Nothing))       Console.WriteLIne(t.InvokeMember("GetName", iFlags, Nothing, _                         oContact, Nothing))       Console.WriteLine(t.InvokeMember("GetCity", iFlags, Nothing, _                         oContact, Nothing))    End Sub End Module 
   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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