MSDN Library
콘텐츠의 테이블 축소
콘텐츠의 테이블 확장
정보
요청한 주제가 아래에 표시됩니다. 그러나 이 주제는 이 라이브러리에 포함되지 않습니다.

Attribute.Equals 메서드

2013-12-13

이 API는 .NET Framework 인프라를 지원하며 사용자 코드에서 직접 사용할 수 없습니다.

이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

Namespace:  System
어셈블리:  mscorlib(mscorlib.dll)

public override bool Equals(
	Object obj
)

매개 변수

obj
형식: System.Object
이 인스턴스와 비교할 Object 또는 null입니다.

반환 값

형식: System.Boolean
obj가 이 인스턴스의 형식 및 값과 같으면 true이고, 그렇지 않으면 false입니다.

다음 코드 예제에서는 두 개의 사용자 지정 매개 변수 Attribute 클래스를 정의하고 각 클래스의 여러 개체를 만든 다음 Equals 메서드를 사용하여 각각을 비교하는 방법을 보여 줍니다.


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

// 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 Example
{
    // Create Attribute objects and compare them.
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        outputBlock.Text += "This example of Attribute.Equals( object ) " +
            "generates the following output." + "\n";

        // 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.
            outputBlock.Text += "\nCompare a usage attribute instance to " +
                "another instance of the same attribute:" + "\n";
            outputBlock.Text += String.Format("   \"{0}\" == \n   \"{1}\" ? {2}",
                arrayUsageAttr1.ToString(), arrayUsageAttr2.ToString(),
                arrayUsageAttr1.Equals(arrayUsageAttr2)) + "\n";

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

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

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

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

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

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

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

Compare an ID attribute to another ID attribute:
   "ArgumentIDAttribute.06abf046-0c38-47ac-b215-09e1daa7f37d" ==
   "ArgumentIDAttribute.bdeb6f3e-18aa-410b-bef6-9788956b008c" ? False
*/


Windows Phone OS

지원되는 버전: 8.0, 7.1, 7.0

Windows Phone

표시:
© 2016 Microsoft