Код IT
← Каталог

Справочник по C# — 3. Пользовательские атрибуты

Фрагмент из «Справочник по C#»: 3. Пользовательские атрибуты.

C# main.cs
[AttributeUsage(
    AttributeTargets.Class | AttributeTargets.Method,
    AllowMultiple = false,
    Inherited = true)]
public sealed class MyAttribute : Attribute
{
    public string Name { get; }
    public int Priority { get; set; } = 0;

    public MyAttribute(string name)
    {
        Name = name ?? throw new ArgumentNullException(nameof(name));
    }
}
[AttributeUsage(
    AttributeTargets.Class | AttributeTargets.Method,
    AllowMultiple = false,
    Inherited = true)]
public sealed class MyAttribute : Attribute
{
    public string Name { get; }
    public int Priority { get; set; } = 0;

    public MyAttribute(string name)
    {
        Name = name ?? throw new ArgumentNullException(nameof(name));
    }
}