信息
您所需的主题如下所示。但此主题未包含在此库中。

Attribute.Match 方法

2013/12/13

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

public virtual bool Match(
	Object obj
)

参数

obj
类型: System.Object
Attribute 的此实例进行比较的 Object

返回值

类型: System.Boolean
如果此实例等于 obj,则为 true;否则为 false

此方法确定两个 Attribute 是否相等。其默认实现与执行值引用比较的 Equals 相同。重写此方法以实现对由本身有意义的组件组成的特性值(如标志或位域)的支持。

例如,请考虑一个值为被分成标志位域的二进制字段的特性。该特性的两个实例的标志集一个共有的,而所有其他标志都不同。Equals 方法无法确定这两个实例是否具有相同的标志集,但 Match 方法可以确定。

下面的代码示例演示在 Attribute 上下文中 Match 的用法。


using System;
using System.Reflection;

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

  protected string authorName1;
  protected string authorName2;

  public string AuthorName1
  {
     get { return authorName1; }
     set { authorName1 = AuthorName1; }
  }

  public string AuthorName2
  {
     get { return authorName2; }
     set { authorName2 = AuthorName2; }
  }

  // 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;

     if (obj is AuthorsAttribute)
        // Combine the hash codes and see if they're unchanged.
        return (((AuthorsAttribute)obj).GetHashCode() & GetHashCode())
           == GetHashCode();
     else
        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()
  { }
}

class Example
{
  public static void Demo(System.Windows.Controls.TextBlock outputBlock)
  {
     // 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 (MethodInfo mInfo1 in clsType1.GetMethods())
     {
        // Check each method for the Authors attribute.
        AuthorsAttribute authAttr1 = (AuthorsAttribute)
           Attribute.GetCustomAttribute(mInfo1,
           typeof(AuthorsAttribute));
        if (authAttr1 != null)
        {
           // Display the authors.
           outputBlock.Text += String.Format("Method {0} was authored by {1} " +
                          "and {2}.", mInfo1.Name,
                          authAttr1.AuthorName1,
                          authAttr1.AuthorName2) + "\n";
           // Iterate through each method of the second class.
           foreach (MethodInfo mInfo2 in clsType2.GetMethods())
           {
              // Check each method for the Authors attribute.
              AuthorsAttribute authAttr2 = (AuthorsAttribute)
                 Attribute.GetCustomAttribute(mInfo2,
                 typeof(AuthorsAttribute));
              // Compare with the authors in the first class.
              if (authAttr2 != null && authAttr2.Match(authAttr1))
                 outputBlock.Text += String.Format("Method {0} in class {1} " +
                    "was authored by the same team.",
                    mInfo2.Name, clsType2.Name) + "\n";
           }
           outputBlock.Text += "" + "\n";
        }
     }
  }
}

/*
 * 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.
 */


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: