当前位置: 首页>>代码示例>>C#>>正文


C# Attribute.Match方法代码示例

本文整理汇总了C#中System.Attribute.Match方法的典型用法代码示例。如果您正苦于以下问题:C# Attribute.Match方法的具体用法?C# Attribute.Match怎么用?C# Attribute.Match使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Attribute的用法示例。


在下文中一共展示了Attribute.Match方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AuthorsAttribute

//引入命名空间
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();
            }
        }
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:129,代码来源:Attribute.Match

输出:

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


注:本文中的System.Attribute.Match方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。