AttributeUsage attribute execution for received attribute types

Given the following, I would not expect the compiler to allow multiple attributes that are derived from the base attribute if AllowMultiple = false is set. It actually compiles without problems - what am I missing here?

using System;

[AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=true)]
abstract class BaseAttribute : Attribute { }

sealed class DerivedAttributeA : BaseAttribute { }

sealed class DerivedAttributeB : BaseAttribute { }

    class Sample1
    {
        [DerivedAttributeA()]
        [DerivedAttributeB()]
        public string PropertyA{ get; set; } // allowed, concrete classes differ

        [DerivedAttributeA()]
        [DerivedAttributeA()]
        public string PropertyB { get; set; } // not allowed, concrete classes the same, honours AllowMultiple=false on BaseAttribute
    }
+5
source share
1 answer

The problem is that validation AllowMultipleonly compares attributes of the same actual type (i.e., a specific instance type) - and perhaps best used with attributes sealedfor this reason.

He, for example, will apply the following (as an illegal duplicate), inheriting this from BaseAttribute:

[DerivedAttributeB()]
[DerivedAttributeB()]
public string Name { get; set; }

, , , ... ( , BaseAttribute ).

:

[Description("abc")]
[I18NDescriptionAttribute("abc")]
public string Name { get; set; }

class I18NDescriptionAttribute : DescriptionAttribute {
    public I18NDescriptionAttribute(string resxKey) : base(resxKey) { } 
}

, [Description] resx ( ComponentModel ..), [Description].. p >

+6

All Articles