Export (0) Print
Expand All

Attribute.Equals Method (Object)

This method supports the .NET Framework 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 boolean Equals (
	Object obj
)
public override function Equals (
	obj : Object
) : boolean
Not applicable.

Parameters

obj

An Object to compare with this instance or a null reference (Nothing in Visual Basic).

Return Value

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.
package NDP_UE_JSL; 

import System.*;
import System.Reflection.*;
   
// Define a custom parameter attribute that takes a single message argument.
/** @attribute AttributeUsage(AttributeTargets.Parameter)
 */
public class ArgumentUsageAttribute extends Attribute
{
    // This is the attribute constructor.
    public ArgumentUsageAttribute(String usgMsg)
    {
        this.usageMsg = usgMsg;
    } //ArgumentUsageAttribute

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

    // Override ToString() to append the message to what the base generates.
    public String ToString()
    {
        return super.ToString() + ":" + usageMsg;
    } //ToString
} //ArgumentUsageAttribute
      
// Define a custom parameter attribute that generates 
// a GUID for each instance.
/** @attribute AttributeUsage(AttributeTargets.Parameter)
 */
public class ArgumentIDAttribute extends Attribute
{
    // This is the attribute constructor, which generates the GUID.
    public ArgumentIDAttribute()
    {
        this.instanceGUID = Guid.NewGuid();
    } //ArgumentIDAttribute

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

    // Override ToString() to append the GUID to what the base generates.
    public String ToString()
    {
        return super.ToString() + "." + instanceGUID.ToString();
    } //ToString
} //ArgumentIDAttribute
  
public class TestClass
{
    // Assign an ArgumentID attribute to each parameter.
    // Assign an ArgumentUsage attribute to each parameter.
    public void TestMethod(
        /** @attribute ArgumentID() 
         */
        /** @attribute ArgumentUsage("Must pass an array here.") 
         */
        String strArray[],
        /** @attribute ArgumentID() 
         */
        /** @attribute ArgumentUsage("Can pass param list or array here.") 
         */
        /** @attribute System.ParamArray() 
         */
        String strList[])
    { } //TestMethod
} //TestClass  
   
class AttributeEqualsDemo
{
    // Create Attribute objects and compare them.
    public static void main(String[] args)
    {
        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 = TestClass.class.ToType();
        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((ParameterInfo)pInfoArray[0], 
                ArgumentUsageAttribute.class.ToType())));

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

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

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

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

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

            // 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(),
                (System.Boolean)arrayUsageAttr1.Equals(arrayUsageAttr2));
            Console.WriteLine(("\nCompare a usage attribute to " 
                + "another usage attribute:"));
            Console.WriteLine("   \"{0}\" == \n   \"{1}\" ? {2}",
                arrayUsageAttr1.ToString(), listUsageAttr.ToString(),
                (System.Boolean)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(),
                (System.Boolean)arrayIDAttr1.Equals(arrayIDAttr2));
            Console.WriteLine(
                "\nCompare an ID attribute to another ID attribute:");
            Console.WriteLine("   \"{0}\" == \n   \"{1}\" ? {2}",
                arrayIDAttr1.ToString(), listIDAttr.ToString(),
                (System.Boolean)arrayIDAttr1.Equals(listIDAttr));
        }
        else {
            Console.WriteLine("The parameters information could " 
                + "not be retrieved for method {0}.", mInfo.get_Name());
        }
    } //main
} //AttributeEqualsDemo

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

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

Compare a usage attribute to another usage attribute:
   "NDP_UE_JSL.ArgumentUsageAttribute@c10548ee:Must pass an array here." ==
   "NDP_UE_JSL.ArgumentUsageAttribute@44a4473c:Can pass param list or array here
." ? False

Compare an ID attribute instance to another instance of the same attribute:
   "NDP_UE_JSL.ArgumentIDAttribute@ed265f4b.bf9a1ba4-0ebc-4472-817a-5ca23520aa9d
" ==
   "NDP_UE_JSL.ArgumentIDAttribute@bb8f099e.024b418b-3dc4-48a6-94bf-84095e53bdb3
" ? False

Compare an ID attribute to another ID attribute:
   "NDP_UE_JSL.ArgumentIDAttribute@ed265f4b.bf9a1ba4-0ebc-4472-817a-5ca23520aa9d
" ==
   "NDP_UE_JSL.ArgumentIDAttribute@75498355.f782cc0d-cbcb-4f6b-a2fd-498fd4db5a33
" ? False

*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft