.NET Framework Class Library
AttributeMatch 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)
Syntax
Public Overridable Function Match ( _
	obj As [%$TOPIC/wy7chz44_en-us_VS_110_1_0_0_0_0%] _
) As [%$TOPIC/wy7chz44_en-us_VS_110_1_0_0_0_1%]
public virtual [%$TOPIC/wy7chz44_en-us_VS_110_1_0_1_0_0%] Match(
	[%$TOPIC/wy7chz44_en-us_VS_110_1_0_1_0_1%] obj
)
public:
virtual [%$TOPIC/wy7chz44_en-us_VS_110_1_0_2_0_0%] Match(
	[%$TOPIC/wy7chz44_en-us_VS_110_1_0_2_0_1%]^ obj
)
abstract Match : 
        obj:[%$TOPIC/wy7chz44_en-us_VS_110_1_0_3_0_0%] -> [%$TOPIC/wy7chz44_en-us_VS_110_1_0_3_0_1%]  
override Match : 
        obj:[%$TOPIC/wy7chz44_en-us_VS_110_1_0_3_0_2%] -> [%$TOPIC/wy7chz44_en-us_VS_110_1_0_3_0_3%]

Parameters

obj
Type: SystemObject

An Object to compare with this instance of Attribute.

Return Value

Type: SystemBoolean
true if this instance equals obj; otherwise, false.
Remarks

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.

Examples

The following code example illustrates the use of Match in the context of 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.
using System;
using System.Reflection;

// A custom attribute to allow 2 authors per method.
[AttributeUsage(AttributeTargets.Method)]
public class AuthorsAttribute : Attribute 
{
	protected string _authorName1;
	protected string _authorName2;

	public AuthorsAttribute(string name1, string name2) 
    {
		_authorName1 = name1;
		_authorName2 = name2;
	}

	public string AuthorName1 
    {
		get { return _authorName1; }
		set { _authorName1 = value; }
	}

	public string AuthorName2 
    {
		get { return _authorName2; }
		set { _authorName2 = value; }
	}


    public override bool Equals(Object obj)
    {
      AuthorsAttribute auth = obj as AuthorsAttribute;
      if (auth == null) return false;

      return ((_authorName1 == auth.AuthorName1 & 
              _authorName2 == auth.AuthorName2) |
              (_authorName1 == auth.AuthorName2 &
              _authorName2 == auth.AuthorName1));
   }

	// Use the hash code of the string objects and xor them together. 
	public override int GetHashCode() 
    {
		return _authorName1.GetHashCode() ^ _authorName2.GetHashCode();
	}

	// Determine if the object is a match to this one. 
	public override bool Match(object obj) 
    {
		// Obviously a match. 
		if (obj == this)
			return true;

		// Obviously we're not null, so no. 
		if (obj == null)
			return false;

		AuthorsAttribute authObj = obj as AuthorsAttribute;
		if (authObj != null)  
            // Check for identical order. 
            if ((_authorName1 == authObj._authorName1) &
                (_authorName2 == authObj._authorName2))
                return true;
            // Check for reversed order. 
            else if ((_authorName1 == authObj._authorName2) &
                (_authorName2 == authObj._authorName1))
                return true;
            else 
                return false;
        else 
    		return false;
	}
}

// Add some authors to methods of a class. 
public class TestClass1 {
	[Authors("William Shakespeare", "Herman Melville")]
	public void Method1()
	{}

	[Authors("Leo Tolstoy", "John Milton")]
	public void Method2()
	{}
}

// Add authors to a second class's methods. 
public class TestClass2 {
	[Authors("William Shakespeare", "Herman Melville")]
	public void Method1()
	{}

	[Authors("Leo Tolstoy", "John Milton")]
	public void Method2()
	{}

	[Authors("William Shakespeare", "John Milton")]
	public void Method3()
	{}

   [Authors("John Milton", "Leo Tolstoy")]
   public void Method4()
   {}
}

class DemoClass {
	static void Main(string[] args) {
		// Get the type for both classes to access their metadata.
		Type clsType1 = typeof(TestClass1);
		Type clsType2 = typeof(TestClass2);

		// Iterate through each method of the first class. 
		foreach(var method in clsType1.GetMethods()) {
			// Check each method for the Authors attribute.
			AuthorsAttribute authAttr1 = (AuthorsAttribute)
				Attribute.GetCustomAttribute(method, 
				typeof(AuthorsAttribute));
			if (authAttr1 != null) {
				// 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. 
				foreach(var method2 in clsType2.GetMethods()) {
					// Check each method for the Authors attribute.
					AuthorsAttribute authAttr2 = (AuthorsAttribute)
						Attribute.GetCustomAttribute(method2, 
						typeof(AuthorsAttribute));
					// Compare with the authors in the first class. 
					if (authAttr2 != null && authAttr2.Match(authAttr1))
						Console.WriteLine("{0}.{1} was also authored by the same team.", 
                                       clsType2.Name, method2.Name);
				}
				Console.WriteLine();
			}
		}
	}
}
// 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.
Version Information

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

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 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.