This documentation is archived and is not being maintained.

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
)

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.


using namespace System;
using namespace System::Reflection;

// A custom attribute to allow 2 authors per method.
public ref class AuthorsAttribute: public Attribute
{
public:
   AuthorsAttribute( String^ name1, String^ name2 )
   {
      authorName1 = name1;
      authorName2 = name2;
   }

protected:
   String^ authorName1;
   String^ authorName2;

public:

   property String^ AuthorName1 
   {
      String^ get()
      {
         return authorName1;
      }

      void set( String^ value )
      {
         authorName1 = value;
      }
   }

   property String^ AuthorName2 
   {
      String^ get()
      {
         return authorName2;
      }

      void set( String^ value )
      {
         authorName2 = value;
      }
   }

   // Use the hash code of the string objects and xor them together.
   virtual int GetHashCode() override
   {
      return authorName1->GetHashCode() ^ authorName2->GetHashCode();
   }

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

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

      if ( dynamic_cast<AuthorsAttribute^>(obj) )

      // Combine the hash codes and see if they're unchanged.
      return (dynamic_cast<AuthorsAttribute^>(obj)->GetHashCode() & GetHashCode()) == GetHashCode();
      else
            return false;
   }
};


// Add some authors to methods of a class.
public ref class TestClass1
{
public:

   [Authors("William Shakespeare","Herman Melville")]
   void Method1(){}


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

};


// Add authors to a second class's methods.
public ref class TestClass2
{
public:

   [Authors("William Shakespeare","Herman Melville")]
   void Method1(){}


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


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

};

int main()
{
   // Get the type for both classes to access their metadata.
   Type^ clsType1 = TestClass1::typeid;
   Type^ clsType2 = TestClass2::typeid;

   // Iterate through each method of the first class.
   System::Collections::IEnumerator^ myEnum = clsType1->GetMethods()->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      MethodInfo^ mInfo1 = safe_cast<MethodInfo^>(myEnum->Current);

      // Check each method for the Authors attribute.
      AuthorsAttribute^ authAttr1 = dynamic_cast<AuthorsAttribute^>(Attribute::GetCustomAttribute( mInfo1, AuthorsAttribute::typeid ));
      if ( authAttr1 != nullptr )
      {
         // Display the authors.
         Console::WriteLine( "Method {0} was authored by {1} and {2}.", mInfo1->Name, authAttr1->AuthorName1, authAttr1->AuthorName2 );

         // Iterate through each method of the second class.
         System::Collections::IEnumerator^ myEnum1 = clsType2->GetMethods()->GetEnumerator();
         while ( myEnum1->MoveNext() )
         {
            MethodInfo^ mInfo2 = safe_cast<MethodInfo^>(myEnum1->Current);

            // Check each method for the Authors attribute.
            AuthorsAttribute^ authAttr2 = dynamic_cast<AuthorsAttribute^>(Attribute::GetCustomAttribute( mInfo2, AuthorsAttribute::typeid ));

            // Compare with the authors in the first class.
            if ( authAttr2 != nullptr && authAttr2->Match(authAttr1) )
                        Console::WriteLine( "Method {0} in class {1} was authored by the same team.", mInfo2->Name, clsType2->Name );
         }
         Console::WriteLine();
      }
   }
}

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


.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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