Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Attribute.Match Method (Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2017 Microsoft