Exemplo n.º 1
0
    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();
            }
        }
    }
    static void Main(string[] args)
    {
        // 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 (var method in clsType1.GetMethods())
        {
            // Check each method for the Authors attribute.
            AuthorsAttribute authAttr1 = (AuthorsAttribute)
                                         Attribute.GetCustomAttribute(method,
                                                                      typeof(AuthorsAttribute));
            if (authAttr1 != null)
            {
                // Display the authors.
                Console.WriteLine("{0}.{1} was authored by {2} and {3}.",
                                  clsType1.Name, method.Name, authAttr1.AuthorName1,
                                  authAttr1.AuthorName2);
                // Iterate through each method of the second class.
                foreach (var method2 in clsType2.GetMethods())
                {
                    // Check each method for the Authors attribute.
                    AuthorsAttribute authAttr2 = (AuthorsAttribute)
                                                 Attribute.GetCustomAttribute(method2,
                                                                              typeof(AuthorsAttribute));
                    // Compare with the authors in the first class.
                    if (authAttr2 != null && authAttr2.Match(authAttr1))
                    {
                        Console.WriteLine("{0}.{1} was also authored by the same team.",
                                          clsType2.Name, method2.Name);
                    }
                }
                Console.WriteLine();
            }
        }
    }