C#
Создаем новое C# консольное приложение... и напишем код
using System;
using System.Linq;
using System.ComponentModel.DataAnnotations; // библиотека для атрибутов
namespace ConsoleApp1
{
// class Book
public class Book
{
[Display(Name = "Displaying something")] // attribute Display
public void Show()
{
Console.WriteLine("Hello");
}
}
// main
class Program
{
public static T GetAttribute<T>(object value, string memberName) where T : Attribute
{
var type = value.GetType();
var memberInfo = type.GetMember(memberName);
var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
return (T)attributes.FirstOrDefault();
}
static void Main(string[] args)
{
Book b = new Book();
// get attribute for method Show
string attrName = GetAttribute<DisplayAttribute>(b, nameof(Book.Show)).GetName();
// attrName = "Displaying something"
}
}
}