Attribute.Match(Object) 方法

定义

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

C#
public virtual bool Match(object? obj);
C#
public virtual bool Match(object obj);

参数

obj
Object

Object 的此实例进行比较的 Attribute

返回

如果该实例等于 obj,则为 true;否则,为 false

注解

此方法确定一个是否等于另一个 Attribute 。 其默认实现与 Equals相同,后者确定两个属性的类型是否相同且字段值相同。

通常, Equals 方法旨在执行引用或值相等性的标准测试。 如果要对相等性执行自定义比较时, Match 可以重写 方法,该比较基于一些条件,但两个属性实例属于同一类型且具有相同的值。 例如,可以重写 方法以 Match 比较以下方案中的属性:

  • 属性包含一个位字段,如果设置了特定位,则认为两个属性实例相等。 例如, NumericDisplay 特性可能包含一个位字段,该字段指示客户端支持的二进制、八进制、十进制和十六进制) 等 (数字格式。 如果两个实例支持相同的数值格式,则重写 Match 的方法可能会认为它们相等。

  • 特性包含多个包含相同类型信息的字段,或者它包含一个数组,其中值可以按任意顺序排列。 例如,一个 Author 属性可能包含作者姓名的多个字段。 如果两个实例具有相同的作者,则重写 Match 的方法可能会认为两个实例相等,而不考虑每个字段是否等于相应的字段。

示例

以下示例演示了如何使用 Match 为属性值创建自定义比较方法。 如果 定义一个 AuthorsAttribute ,它内部包含 List<String> 存储作者姓名的 。 由于名称可以按列表中的任何顺序出现,因此它会重写 Match 方法以比较作者姓名,而不考虑作者姓名在列表中的位置。 请注意, Equals 执行值相等性测试的 方法从 方法返回不同的结果 Match

C#
using System;
using System.Collections.Generic;
using System.Reflection;

// A custom attribute to allow multiple authors per method.
[AttributeUsage(AttributeTargets.Method)]
public class AuthorsAttribute : Attribute
{
   protected List<string> _authors;

    public AuthorsAttribute(params string[] names)
   {
      _authors = new List<string>(names);
    }

   public List<string> Authors
   {
       get { return _authors; }
   }

    // Determine if the object is a match to this one.
    public override bool Match(object obj)
   {
      // Return false if obj is null or not an AuthorsAttribute.
      AuthorsAttribute authors2 = obj as AuthorsAttribute;
      if (authors2 == null) return false;

        // Return true if obj and this instance are the same object reference.
      if (Object.ReferenceEquals(this, authors2))
         return true;

      // Return false if obj and this instance have different numbers of authors
      if (_authors.Count != authors2._authors.Count)
         return false;

      bool matches = false;
      foreach (var author in _authors)
      {
         for (int ctr = 0; ctr < authors2._authors.Count; ctr++)
         {
            if (author == authors2._authors[ctr])
            {
               matches = true;
               break;
            }
            if (ctr == authors2._authors.Count)
            {
               matches = false;
            }
         }
      }
      return matches;
   }

   public override string ToString()
   {
      string retval = "";
      for (int ctr = 0; ctr < _authors.Count; ctr++)
      {
         retval += $"{_authors[ctr]}{(ctr < _authors.Count - 1 ? ", " : String.Empty)}";
      }
      if (retval.Trim().Length == 0)
      {
         return "<unknown>";
      }
      return retval;
   }
}

// Add some authors to methods of a class.
public class TestClass {
    [Authors("Leo Tolstoy", "John Milton")]
    public void Method1()
    {}

    [Authors("Anonymous")]
    public void Method2()
    {}

    [Authors("Leo Tolstoy", "John Milton", "Nathaniel Hawthorne")]
    public void Method3()
    {}

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

class Example
{
    static void Main()
   {
        // Get the type for TestClass to access its metadata.
        Type clsType = typeof(TestClass);

        // Iterate through each method of the class.
      AuthorsAttribute authors = null;
        foreach(var method in clsType.GetMethods())
      {
            // Check each method for the Authors attribute.
            AuthorsAttribute authAttr = (AuthorsAttribute)  Attribute.GetCustomAttribute(method,
                                         typeof(AuthorsAttribute));
            if (authAttr != null)
         {
            // Display the authors.
                Console.WriteLine($"{clsType.Name}.{method.Name} was authored by {authAttr}.");

            // Select Method1's authors as the basis for comparison.
            if (method.Name == "Method1")
            {
                   authors = authAttr;
               Console.WriteLine();
               continue;
            }

            // Compare first authors with the authors of this method.
            if (authors.Match(authAttr))
            {
                    Console.WriteLine("TestClass.Method1 was also authored by the same team.");
            }

            // Perform an equality comparison of the two attributes.
            Console.WriteLine($"{authors} {(authors.Equals(authAttr) ? "=" : "<>")} {authAttr}");
            Console.WriteLine();
            }
        }
    }
}
// The example displays the following output:
//       TestClass.Method1 was authored by Leo Tolstoy, John Milton.
//
//       TestClass.Method2 was authored by Anonymous.
//       Leo Tolstoy, John Milton <> Anonymous
//
//       TestClass.Method3 was authored by Leo Tolstoy, John Milton, Nathaniel Hawthorne.
//       Leo Tolstoy, John Milton <> Leo Tolstoy, John Milton, Nathaniel Hawthorne
//
//       TestClass.Method4 was authored by John Milton, Leo Tolstoy.
//       TestClass.Method1 was also authored by the same team.
//       Leo Tolstoy, John Milton <> John Milton, Leo Tolstoy

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1