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)

public virtual bool Match(
	Object 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.

using System;
using System.Reflection;

// A custom attribute to allow 2 authors per 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;
                return false;
    		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)
			if (authAttr1 != null) {
				// Display the authors.
				Console.WriteLine("{0}.{1} was authored by {2} and {3}.", 
                                  clsType1.Name, method.Name, authAttr1.AuthorName1, 
				// Iterate through each method of the second class. 
				foreach(var method2 in clsType2.GetMethods()) {
					// Check each method for the Authors attribute.
					AuthorsAttribute authAttr2 = (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);
// 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

Supported in: 4.5.2, 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

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft