Пример #1
0
        public void PrintMethodsByAuthor()
        {
            Type metadata = typeof(StartUp);

            var methods = metadata
                          .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

            foreach (var method in methods)
            {
                var attributes = method.GetCustomAttributes(false);

                foreach (var attribute in attributes)
                {
                    AuthorAttribute author = attribute as AuthorAttribute;

                    if (author != null)
                    {
                        Console.WriteLine($"{method.Name} is written by {author.Name}");
                    }
                }
            }
        }
Пример #2
0
        private void PrintAllMethodAuthors(Type type)
        {
            MethodInfo[] methods = type.GetMethods((BindingFlags)60);
            foreach (var method in methods)
            {
                if (!method.GetCustomAttributes().Any(a => a.GetType() == typeof(AuthorAttribute)))
                {
                    continue;
                }

                Attribute[] attributes = method.GetCustomAttributes().ToArray();

                foreach (var attr in attributes)
                {
                    if (attr is AuthorAttribute)
                    {
                        AuthorAttribute author = (AuthorAttribute)attr;
                        Console.WriteLine($"{method.Name} is written by {author.Name}");
                    }
                }
            }
        }