AttributeEquals Method
 
System_CAPS_noteNote

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

This API supports the product infrastructure and is not intended to be used directly from your code.

Returns a value that indicates whether this instance is equal to a specified object.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public override bool Equals(
	object obj
)
public:
virtual bool Equals(
	Object^ obj
) override
override Equals : 
        obj:Object -> bool
Public Overrides Function Equals (
	obj As Object
) As Boolean

Parameters

obj
Type:

An Object to compare with this instance or null.

Return Value

Type:

true if obj equals the type and value of this instance; otherwise, false.

The following code example defines two custom parameter Attribute classes, then creates several objects of each class and shows the use of the Equals method to compare them.

// Example for the Attribute.Equals( object ) method.
using System;
using System.Reflection;

namespace NDP_UE_CS 
{
    // Define a custom parameter attribute that takes a single message argument.
    [AttributeUsage( AttributeTargets.Parameter )]
    public class ArgumentUsageAttribute : Attribute
    {
        // This is the attribute constructor.
        public ArgumentUsageAttribute( string UsageMsg )
        {
            this.usageMsg = UsageMsg;
        }

        // usageMsg is storage for the attribute message.
        protected string usageMsg;

        // Override ToString() to append the message to what the base generates.
        public override string ToString( )
        {
            return base.ToString( ) + ":" + usageMsg;
        }
    }

    // Define a custom parameter attribute that generates 
    // a GUID for each instance.
    [AttributeUsage( AttributeTargets.Parameter )]
    public class ArgumentIDAttribute : Attribute
    {
        // This is the attribute constructor, which generates the GUID.
        public ArgumentIDAttribute( )
        {
            this.instanceGUID = Guid.NewGuid( );
        }

        // instanceGUID is storage for the generated GUID.
        protected Guid instanceGUID;

        // Override ToString() to append the GUID to what the base generates.
        public override string ToString( )
        {
            return base.ToString( ) + "." + instanceGUID.ToString( );
        }
    }

    public class TestClass 
    {
        // Assign an ArgumentID attribute to each parameter.
        // Assign an ArgumentUsage attribute to each parameter.
        public void TestMethod(
            [ArgumentID]
            [ArgumentUsage("Must pass an array here.")]
            String[] strArray,
            [ArgumentID]
            [ArgumentUsage("Can pass param list or array here.")]
            params String[] strList)
        { }
    }

    class AttributeEqualsDemo 
    {
        // Create Attribute objects and compare them.
        static void Main( ) 
        {
            Console.WriteLine( "This example of Attribute.Equals( object ) " +
                "generates the following output." );

            // Get the class type, and then get the MethodInfo object 
            // for TestMethod to access its metadata.
            Type clsType = typeof( TestClass );
            MethodInfo mInfo = clsType.GetMethod("TestMethod");

            // There will be two elements in pInfoArray, one for each parameter.
            ParameterInfo[] pInfoArray = mInfo.GetParameters();
            if (pInfoArray != null) 
            {
                // Create an instance of the argument usage attribute on strArray.
                ArgumentUsageAttribute arrayUsageAttr1 = (ArgumentUsageAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[0], 
                        typeof(ArgumentUsageAttribute) );

                // Create another instance of the argument usage attribute 
                // on strArray.
                ArgumentUsageAttribute arrayUsageAttr2 = (ArgumentUsageAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[0], 
                        typeof(ArgumentUsageAttribute) );

                // Create an instance of the argument usage attribute on strList.
                ArgumentUsageAttribute listUsageAttr = (ArgumentUsageAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[1], 
                        typeof(ArgumentUsageAttribute) );

                // Create an instance of the argument ID attribute on strArray.
                ArgumentIDAttribute arrayIDAttr1 = (ArgumentIDAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[0], 
                        typeof(ArgumentIDAttribute) );

                // Create another instance of the argument ID attribute on strArray.
                ArgumentIDAttribute arrayIDAttr2 = (ArgumentIDAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[0], 
                        typeof(ArgumentIDAttribute) );

                // Create an instance of the argument ID attribute on strList.
                ArgumentIDAttribute listIDAttr = (ArgumentIDAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[1], 
                        typeof(ArgumentIDAttribute) );

                // Compare various pairs of attributes for equality.
                Console.WriteLine( "\nCompare a usage attribute instance to " +
                    "another instance of the same attribute:" );
                Console.WriteLine( "   \"{0}\" == \n   \"{1}\" ? {2}",
                    arrayUsageAttr1.ToString(), arrayUsageAttr2.ToString(), 
                    arrayUsageAttr1.Equals( arrayUsageAttr2 ) );

                Console.WriteLine( "\nCompare a usage attribute to " +
                    "another usage attribute:" );
                Console.WriteLine( "   \"{0}\" == \n   \"{1}\" ? {2}",
                    arrayUsageAttr1.ToString(), listUsageAttr.ToString(), 
                    arrayUsageAttr1.Equals( listUsageAttr ) );

                Console.WriteLine( "\nCompare an ID attribute instance to " +
                    "another instance of the same attribute:" );
                Console.WriteLine( "   \"{0}\" == \n   \"{1}\" ? {2}",
                    arrayIDAttr1.ToString(), arrayIDAttr2.ToString(), 
                    arrayIDAttr1.Equals( arrayIDAttr2 ) );

                Console.WriteLine( "\nCompare an ID attribute to another ID attribute:" );
                Console.WriteLine( "   \"{0}\" == \n   \"{1}\" ? {2}",
                    arrayIDAttr1.ToString(), listIDAttr.ToString(), 
                    arrayIDAttr1.Equals( listIDAttr ) );
            }
            else
                Console.WriteLine( "The parameters information could " +
                    "not be retrieved for method {0}.", mInfo.Name);
        }
    }
}

/*
This example of Attribute.Equals( object ) generates the following output.

Compare a usage attribute instance to another instance of the same attribute:
   "NDP_UE_CS.ArgumentUsageAttribute:Must pass an array here." ==
   "NDP_UE_CS.ArgumentUsageAttribute:Must pass an array here." ? True

Compare a usage attribute to another usage attribute:
   "NDP_UE_CS.ArgumentUsageAttribute:Must pass an array here." ==
   "NDP_UE_CS.ArgumentUsageAttribute:Can pass param list or array here." ? False

Compare an ID attribute instance to another instance of the same attribute:
   "NDP_UE_CS.ArgumentIDAttribute.06abf046-0c38-47ac-b215-09e1daa7f37d" ==
   "NDP_UE_CS.ArgumentIDAttribute.cea23c39-f14b-4e95-bee2-9f661d8cd64b" ? False

Compare an ID attribute to another ID attribute:
   "NDP_UE_CS.ArgumentIDAttribute.06abf046-0c38-47ac-b215-09e1daa7f37d" ==
   "NDP_UE_CS.ArgumentIDAttribute.bdeb6f3e-18aa-410b-bef6-9788956b008c" ? False
*/
' Example for the Attribute.Equals( Object ) method.
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Define a custom parameter attribute that takes a single message argument.
    <AttributeUsage(AttributeTargets.Parameter)>  _
    Public Class ArgumentUsageAttribute
        Inherits Attribute

        ' This is the attribute constructor.
        Public Sub New(UsageMsg As String)
            Me.usageMsg = UsageMsg
        End Sub ' New

        ' usageMsg is storage for the attribute message.
        Protected usageMsg As String

        ' Override ToString() to append the message to what the base generates.
        Public Overrides Function ToString() As String
            Return MyBase.ToString() + ":" + usageMsg
        End Function ' ToString
    End Class ' ArgumentUsageAttribute

    ' Define a custom parameter attribute that generates a GUID for each instance.
    <AttributeUsage(AttributeTargets.Parameter)>  _
    Public Class ArgumentIDAttribute
        Inherits Attribute

        ' This is the attribute constructor, which generates the GUID.
        Public Sub New()
            Me.GUIDinstance = Guid.NewGuid()
        End Sub ' New

        ' instanceGUID is storage for the generated GUID.
        Protected GUIDinstance As Guid

        ' Override ToString() to append the GUID to what the base generates.
        Public Overrides Function ToString() As String
            Return MyBase.ToString() + "." + GUIDinstance.ToString()
        End Function ' ToString
    End Class ' ArgumentIDAttribute

    Public Class TestClass

        ' Assign an ArgumentID attribute to each parameter.
        ' Assign an ArgumentUsage attribute to each parameter.
        Public Sub TestMethod( _
            <ArgumentID(), ArgumentUsage("Must pass an array here.")> _
            strArray() As String, _
            <ArgumentID(), ArgumentUsage("Can pass param list or array here.")> _
            ParamArray strList() As String)
        End Sub ' TestMethod
    End Class ' TestClass

    Module AttributeEqualsDemo

        ' Create Attribute objects and compare them.
        Sub CompareAttributes()

            ' Get the class type, and then get the MethodInfo object 
            ' for TestMethod to access its metadata.
            Dim clsType As Type = GetType(TestClass)
            Dim mInfo As MethodInfo = clsType.GetMethod("TestMethod")

            ' There will be two elements in pInfoArray, one for each parameter.
            Dim pInfoArray As ParameterInfo() = mInfo.GetParameters()
            If Not (pInfoArray Is Nothing) Then

                ' Create an instance of the argument usage attribute on strArray.
                Dim arrayUsageAttr1 As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(0), _
                        GetType(ArgumentUsageAttribute))

                ' Create another instance of the argument usage attribute 
                ' on strArray.
                Dim arrayUsageAttr2 As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(0), _
                        GetType(ArgumentUsageAttribute))

                ' Create an instance of the argument usage attribute on strList.
                Dim listUsageAttr As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(1), _
                        GetType(ArgumentUsageAttribute))

                ' Create an instance of the argument ID attribute on strArray.
                Dim arrayIDAttr1 As ArgumentIDAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(0), _
                        GetType(ArgumentIDAttribute))

                ' Create another instance of the argument ID attribute on strArray.
                Dim arrayIDAttr2 As ArgumentIDAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(0), _
                        GetType(ArgumentIDAttribute))

                ' Create an instance of the argument ID attribute on strList.
                Dim listIDAttr As ArgumentIDAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(1), _
                        GetType(ArgumentIDAttribute))

                ' Compare various pairs of attributes for equality.
                Console.WriteLine(vbCrLf & "Compare a usage attribute instance " & _
                    "to another instance of the same attribute:")
                Console.WriteLine("   ""{0}"" = " & vbCrLf & "   ""{1}"" ? {2}", _
                    arrayUsageAttr1.ToString(), arrayUsageAttr2.ToString(), _
                    arrayUsageAttr1.Equals(arrayUsageAttr2))

                Console.WriteLine(vbCrLf & _
                    "Compare a usage attribute to another usage attribute:")
                Console.WriteLine("   ""{0}"" = " & vbCrLf & "   ""{1}"" ? {2}", _
                    arrayUsageAttr1.ToString(), listUsageAttr.ToString(), _
                    arrayUsageAttr1.Equals(listUsageAttr))

                Console.WriteLine(vbCrLf & "Compare an ID attribute instance " & _
                    "to another instance of the same attribute:")
                Console.WriteLine("   ""{0}"" = " & vbCrLf & "   ""{1}"" ? {2}", _
                    arrayIDAttr1.ToString(), arrayIDAttr2.ToString(), _
                    arrayIDAttr1.Equals(arrayIDAttr2))

                Console.WriteLine(vbCrLf & _
                    "Compare an ID attribute to another ID attribute:")
                Console.WriteLine("   ""{0}"" = " & vbCrLf & "   ""{1}"" ? {2}", _
                    arrayIDAttr1.ToString(), listIDAttr.ToString(), _
                    arrayIDAttr1.Equals(listIDAttr))
            Else
                Console.WriteLine("The parameters information could " & _
                    "not be retrieved for method {0}.", mInfo.Name)
            End If
        End Sub ' CompareAttributes

        Sub Main()
            Console.WriteLine("This example of Attribute.Equals( Object ) " & _
                "generates the following output." )

            CompareAttributes( )

        End Sub ' Main
    End Module ' AttributeEqualsDemo
End Namespace ' NDP_UE_VB

' This example of Attribute.Equals( Object ) generates the following output.
' 
' Compare a usage attribute instance to another instance of the same attribute:
'    "NDP_UE_VB.ArgumentUsageAttribute:Must pass an array here." =
'    "NDP_UE_VB.ArgumentUsageAttribute:Must pass an array here." ? True
' 
' Compare a usage attribute to another usage attribute:
'    "NDP_UE_VB.ArgumentUsageAttribute:Must pass an array here." =
'    "NDP_UE_VB.ArgumentUsageAttribute:Can pass param list or array here." ? False
' 
' Compare an ID attribute instance to another instance of the same attribute:
'    "NDP_UE_VB.ArgumentIDAttribute.aa2c2346-ca87-40d6-afb7-5e3bc1637351" =
'    "NDP_UE_VB.ArgumentIDAttribute.4192c26c-9a7b-4a74-97fc-6c3dfbc2cdfe" ? False
' 
' Compare an ID attribute to another ID attribute:
'    "NDP_UE_VB.ArgumentIDAttribute.aa2c2346-ca87-40d6-afb7-5e3bc1637351" =
'    "NDP_UE_VB.ArgumentIDAttribute.237a7337-15f1-469b-a5ce-7503def917b2" ? False
// Example for the Attribute.Equals( Object* ) method.
using namespace System;
using namespace System::Reflection;

namespace NDP_UE_CPP
{
   // Define a custom parameter attribute that takes a single message argument.

   [AttributeUsage(AttributeTargets::Parameter)]
   public ref class ArgumentUsageAttribute: public Attribute
   {
   protected:

      // usageMsg is storage for the attribute message.
      String^ usageMsg;

   public:

      // This is the attribute constructor.
      ArgumentUsageAttribute( String^ UsageMsg )
      {
         this->usageMsg = UsageMsg;
      }


      // Override ToString() to append the message to what the base generates.
      virtual String^ ToString() override
      {
         return String::Concat( Attribute::ToString(), ":", usageMsg );
      }

   };


   // Define a custom parameter attribute that generates a GUID for each instance.

   [AttributeUsage(AttributeTargets::Parameter)]
   public ref class ArgumentIDAttribute: public Attribute
   {
   protected:

      // instanceGUID is storage for the generated GUID.
      Guid instanceGUID;

   public:

      // This is the attribute constructor, which generates the GUID.
      ArgumentIDAttribute()
      {
         this->instanceGUID = Guid::NewGuid();
      }

      // Override ToString() to append the GUID to what the base generates.
      virtual String^ ToString() override
      {
         return String::Concat( Attribute::ToString(), ".", instanceGUID.ToString() );
      }
   };

   public ref class TestClass
   {
   public:

      // Assign an ArgumentID attribute to each parameter.
      // Assign an ArgumentUsage attribute to each parameter.
      void TestMethod( [ArgumentID][ArgumentUsage("Must pass an array here.")]array<String^>^strArray, 
                       [ArgumentID][ArgumentUsage("Can pass param list or array here.")]array<String^>^strList ){}

   };


   // Create Attribute objects and compare them.
   void CompareAttributes()
   {
      // Get the class type, and then get the MethodInfo object 
      // for TestMethod to access its metadata.
      Type^ clsType = TestClass::typeid;
      MethodInfo^ mInfo = clsType->GetMethod( "TestMethod" );

      // There will be two elements in pInfoArray, one for each parameter.
      array<ParameterInfo^>^pInfoArray = mInfo->GetParameters();
      if ( pInfoArray != nullptr )
      {
         // Create an instance of the argument usage attribute on strArray.
         ArgumentUsageAttribute^ arrayUsageAttr1 = static_cast<ArgumentUsageAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 0 ], ArgumentUsageAttribute::typeid ));

         // Create another instance of the argument usage attribute 
         // on strArray.
         ArgumentUsageAttribute^ arrayUsageAttr2 = static_cast<ArgumentUsageAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 0 ], ArgumentUsageAttribute::typeid ));

         // Create an instance of the argument usage attribute on strList.
         ArgumentUsageAttribute^ listUsageAttr = static_cast<ArgumentUsageAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 1 ], ArgumentUsageAttribute::typeid ));

         // Create an instance of the argument ID attribute on strArray.
         ArgumentIDAttribute^ arrayIDAttr1 = static_cast<ArgumentIDAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 0 ], ArgumentIDAttribute::typeid ));

         // Create another instance of the argument ID attribute on strArray.
         ArgumentIDAttribute^ arrayIDAttr2 = static_cast<ArgumentIDAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 0 ], ArgumentIDAttribute::typeid ));

         // Create an instance of the argument ID attribute on strList.
         ArgumentIDAttribute^ listIDAttr = static_cast<ArgumentIDAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 1 ], ArgumentIDAttribute::typeid ));

         // Compare various pairs of attributes for equality.
         Console::WriteLine( "\nCompare a usage attribute instance to "
         "another instance of the same attribute:" );
         Console::WriteLine( "   \"{0}\" == \n   \"{1}\" ? {2}", arrayUsageAttr1->ToString(), arrayUsageAttr2->ToString(), arrayUsageAttr1->Equals( arrayUsageAttr2 ) );
         Console::WriteLine( "\nCompare a usage attribute to another usage attribute:" );
         Console::WriteLine( "   \"{0}\" == \n   \"{1}\" ? {2}", arrayUsageAttr1->ToString(), listUsageAttr->ToString(), arrayUsageAttr1->Equals( listUsageAttr ) );
         Console::WriteLine( "\nCompare an ID attribute instance to "
         "another instance of the same attribute:" );
         Console::WriteLine( "   \"{0}\" == \n   \"{1}\" ? {2}", arrayIDAttr1->ToString(), arrayIDAttr2->ToString(), arrayIDAttr1->Equals( arrayIDAttr2 ) );
         Console::WriteLine( "\nCompare an ID attribute to another ID attribute:" );
         Console::WriteLine( "   \"{0}\" == \n   \"{1}\" ? {2}", arrayIDAttr1->ToString(), listIDAttr->ToString(), arrayIDAttr1->Equals( listIDAttr ) );
      }
      else
            Console::WriteLine( "The parameters information could "
      "not be retrieved for method {0}.", mInfo->Name );
   }
}

int main()
{
   Console::WriteLine( "This example of Attribute::Equals( Object* ) "
   "generates the following output." );
   NDP_UE_CPP::CompareAttributes();
}

/*
This example of Attribute::Equals( Object* ) generates the following output.

Compare a usage attribute instance to another instance of the same attribute:
   "NDP_UE_CPP.ArgumentUsageAttribute:Must pass an array here." ==
   "NDP_UE_CPP.ArgumentUsageAttribute:Must pass an array here." ? True

Compare a usage attribute to another usage attribute:
   "NDP_UE_CPP.ArgumentUsageAttribute:Must pass an array here." ==
   "NDP_UE_CPP.ArgumentUsageAttribute:Can pass param list or array here." ? False

Compare an ID attribute instance to another instance of the same attribute:
   "NDP_UE_CPP.ArgumentIDAttribute.28db2434-1031-469a-9a13-babeb9be9e2c" ==
   "NDP_UE_CPP.ArgumentIDAttribute.1140dcb9-7341-4ba0-b2de-e9d8f0955216" ? False

Compare an ID attribute to another ID attribute:
   "NDP_UE_CPP.ArgumentIDAttribute.28db2434-1031-469a-9a13-babeb9be9e2c" ==
   "NDP_UE_CPP.ArgumentIDAttribute.7f983425-5ea9-4c90-b536-bd6229fdfe63" ? False
*/
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top