Exportar (0) Imprimir
Expandir todo
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
Traducción
Original

Attribute.Match (Método)

Cuando se invalida en una clase derivada, devuelve un valor que indica si esta instancia es igual al objeto especificado.

Espacio de nombres:  System
Ensamblado:  mscorlib (en mscorlib.dll)

'Declaración
Public Overridable Function Match ( _
	obj As Object _
) As Boolean

Parámetros

obj
Tipo: System.Object
Object que se va a comparar con esta instancia de Attribute.

Valor devuelto

Tipo: System.Boolean
Es true si esta instancia es igual a obj; en caso contrario, es false.

Este método determina si un Attribute es igual a otro. La implementación predeterminada es la misma que Equals, que realiza la comparación de un valor y una referencia. Reemplace este método para implementar funcionalidad para valores de atributo, como marcadores o campos de bits, que constan de componentes con relevancia propia.

Por ejemplo, supongamos que existe un atributo cuyo valor es un campo binario dividido en un campo de bits de marcadores. Dos instancias de este atributo tienen un marcador en común mientras que todos los demás marcadores difieren. El método Equals no puede determinar si las dos instancias tienen el mismo marcador, pero el método Match sí puede.

En el siguiente ejemplo de código se muestra el uso de Match en el contexto de Attribute.


Imports System.Reflection

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

   Public Sub New(name1 As String, name2 As String)
      _authorName1 = name1
      _authorName2 = name2
   End Sub

   Public Property AuthorName1() As String
      Get
         Return _authorName1
      End Get
      Set
         _authorName1 = value
      End Set
   End Property

   Public Property AuthorName2() As String
      Get
         Return _authorName2
      End Get
      Set
         _authorName2 = value
      End Set
   End Property

   Public Overrides Function Equals(obj As Object) As Boolean
      Dim auth As AuthorsAttribute = TryCast(obj, AuthorsAttribute)
      If auth Is Nothing Then Return False

      Return (_authorName1 = obj.AuthorName1 And 
              _authorName2 = obj.AuthorName2) Or
              (_authorName1 = obj.AuthorName2 And
              _authorName2 = obj.AuthorName1)
   End Function

   ' Use the hash code of the string objects and xor them together.
   Public Overrides Function GetHashCode() As Integer
      Return _authorName1.GetHashCode() XOr _authorName2.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

      Dim authObj As AuthorsAttribute = TryCast(obj, AuthorsAttribute)
      If authObj IsNot Nothing Then
         ' Check for identical order.
         If _authorName1 = authObj._authorName1 And
            _authorName2 = authObj._authorName2 Then
            Return True
         ' Check for reversed order.
         Else If _authorName1 = authObj._authorName2 And
            _authorName2 = authObj._authorName1 Then
            Return True
         Else
            Return False
         End If
      Else
         Return False
      End If
   End Function
End Class

' Add some authors to methods of a 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

' Add authors to a second class's methods.
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

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

Public Module Example
   Sub Main()
      ' Get the Type object for both classes.
      Dim clsType1 As Type = GetType(TestClass1)
      Dim clsType2 As Type = GetType(TestClass2)

      ' Iterate through each method of the first class.
      For Each method In clsType1.GetMethods()
         ' Check each method for the Authors attribute.
         Dim attr1 As AuthorsAttribute = CType(Attribute.GetCustomAttribute(method, 
                                  GetType(AuthorsAttribute)), AuthorsAttribute)
         If attr1 IsNot Nothing Then
            Dim authAttr1 As AuthorsAttribute = 
                             CType(attr1, AuthorsAttribute)
            ' Display the authors.
            Console.WriteLine("{0}.{1} was authored by {2} and {3}.", 
                              clsType1.Name, method.Name, authAttr1.AuthorName1, 
                              authAttr1.AuthorName2)
            ' Iterate through each method of the second class.
            For Each method2 In clsType2.GetMethods()
               ' Check each method for the Authors attribute.
               Dim attr2 As AuthorsAttribute = CType(Attribute.GetCustomAttribute( 
                                        method2, GetType(AuthorsAttribute)), 
                                        AuthorsAttribute)
               If attr2 IsNot Nothing Then
                  Dim authAttr2 As AuthorsAttribute = _
                                CType(attr2, AuthorsAttribute)
                  ' Compare with the authors in the first class.
                  If authAttr2.Match(authAttr1) = True Then
                     Console.WriteLine("{0}.{1} was also authored by the same team.", 
                                       clsType2.Name, method2.Name)
                  End If
               End If
            Next
            Console.WriteLine()
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'    TestClass1.Method1 was authored by William Shakespeare and Herman Melville.
'    TestClass2.Method1 was also authored by the same team.
'    
'    TestClass1.Method2 was authored by Leo Tolstoy and John Milton.
'    TestClass2.Method2 was also authored by the same team.
'    TestClass2.Method4 was also authored by the same team.


.NET Framework

Compatible con: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Compatible con: 4, 3.5 SP1

.NET para aplicaciones de Windows Phone

Compatible con: Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (no se admite el rol Server Core), Windows Server 2008 R2 (se admite el rol Server Core con SP1 o versiones posteriores; no se admite Itanium)

.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

Adiciones de comunidad

AGREGAR
Mostrar:
© 2014 Microsoft