Export (0) Print
Expand All

Attribute.Match Method

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

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

'Declaration
Public Overridable Function Match ( _
	obj As Object _
) As Boolean

Parameters

obj
Type: System.Object
An Object to compare with this instance of Attribute.

Return Value

Type: System.Boolean
true if this instance equals obj; otherwise, false.

This method determines if one Attribute equals another. Its default implementation is the same as Equals, which performs a value and reference comparison. Override this method to implement support for attribute values, such as flags or bit fields, that consist of components that are meaningful in themselves.

For example, consider an attribute whose value is a binary field divided into a bit field of flags. Two instances of this attribute have one flag in set in common while all the other flags differ. The Equals method cannot determine that the two instances have the same flag set, but the Match method can.

The following code example illustrates the use of Match in the context of Attribute.


Imports System.Reflection

Module Example

   ' A custom attribute to allow 2 authors per method.
   <AttributeUsage(AttributeTargets.Method)> _
   Public Class AuthorsAttribute
      Inherits Attribute

      Public Sub New(ByVal name1 As String, ByVal name2 As String)
         myAuthorName1 = name1
         myAuthorName2 = name2
      End Sub

      Protected myAuthorName1 As String
      Protected myAuthorName2 As String

      Public Property AuthorName1() As String
         Get
            Return myAuthorName1
         End Get
         Set(ByVal Value As String)
            myAuthorName1 = AuthorName1
         End Set
      End Property

      Public Property AuthorName2() As String
         Get
            Return myAuthorName2
         End Get
         Set(ByVal Value As String)
            myAuthorName2 = AuthorName2
         End Set
      End Property

      ' Use the hash code of the string objects and xor them together.
      Public Overrides Function GetHashCode() As Integer
         Return myAuthorName1.GetHashCode() Xor myAuthorName2.GetHashCode()
      End Function

      ' Determine if the object is a match to this one.
      Public Overrides Function Match(ByVal obj As Object) As Boolean
         ' Obviously a match.
         If obj Is Me Then
            Return True
         End If

         ' Obviously we're not nothing, so no.
         If obj Is Nothing Then
            Return False
         End If

         If TypeOf obj Is AuthorsAttribute Then
            ' Combine the hash codes and see if they're unchanged.
            Dim authObj As AuthorsAttribute = CType(obj, AuthorsAttribute)
            Dim firstHash As Integer = authObj.GetHashCode() And GetHashCode()
            If firstHash = GetHashCode() Then
               Return True
            Else
               Return False
            End If
         Else
            Return False
         End If
      End Function

   End Class
   Public Class TestClass1
      <Authors("William Shakespeare", "Herman Melville")> _
      Public Sub Method1()
      End Sub

      <Authors("Leo Tolstoy", "John Milton")> _
      Public Sub Method2()
      End Sub
   End Class
   Public Class TestClass2
      <Authors("William Shakespeare", "Herman Melville")> _
      Public Sub Method1()
      End Sub

      <Authors("Leo Tolstoy", "John Milton")> _
      Public Sub Method2()
      End Sub

      <Authors("Francis Bacon", "Miguel Cervantes")> _
      Public Sub Method3()
      End Sub
   End Class

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Get the type for both classes to access their metadata.
      Dim clsType1 As Type = GetType(TestClass1)
      Dim clsType2 As Type = GetType(TestClass2)

      Dim mInfo1 As MethodInfo
      ' Iterate through each method of the first class.
      For Each mInfo1 In clsType1.GetMethods()
         ' Check each method for the Authors attribute.
         Dim attr1 As Attribute = Attribute.GetCustomAttribute(mInfo1, _
             GetType(AuthorsAttribute))
         If Not attr1 Is Nothing And TypeOf attr1 Is AuthorsAttribute Then
            Dim authAttr1 As AuthorsAttribute = _
                CType(attr1, AuthorsAttribute)
            ' Display the authors.
            outputBlock.Text &= String.Format("Method {0} was authored by {1} and {2}.", _
                            mInfo1.Name, authAttr1.AuthorName1, _
                            authAttr1.AuthorName2) & vbCrLf
            Dim mInfo2 As MethodInfo
            ' Iterate through each method of the second class.
            For Each mInfo2 In clsType2.GetMethods()
               ' Check each method for the Authors attribute.
               Dim attr2 As Attribute = Attribute.GetCustomAttribute( _
                   mInfo2, GetType(AuthorsAttribute))
               If Not attr2 Is Nothing And _
                   TypeOf attr2 Is AuthorsAttribute Then
                  Dim authAttr2 As AuthorsAttribute = _
                      CType(attr2, AuthorsAttribute)
                  ' Compare with the authors in the first class.
                  If authAttr2.Match(authAttr1) = True Then
                     outputBlock.Text &= String.Format("Method {0} in class {1} was " + _
                                 "also authored by the same team.", _
                                 mInfo2.Name, clsType2.Name) & vbCrLf
                  End If
               End If
            Next
            outputBlock.Text &= "" & vbCrLf
         End If
      Next
   End Sub
End Module

' Output:
' Method Method1 was authored by William Shakespeare and Herman Melville.
' Method Method1 in class TestClass2 was also authored by the same team.
' 
' Method Method2 was authored by Leo Tolstoy and John Milton.
' Method Method2 in class TestClass2 was also authored by the same team.


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft