AllowMultiple not working with property attributes?

I will contact to collect all user attributes placed on top of the property. There are several attributes of the same type assigned to a property, but when they are collected, the resulting collection contains only the first attribute of a certain type:

Attribute class

[AttributeUsage(System.AttributeTargets.Property, AllowMultiple = true)] public class ConditionAttribute : Attribute{...} 

Using:

 [ConditionAttribute("Test1")] [ConditionAttribute("Test2")] [ConditionAttribute("Test3")] public Color BackColor{get; set;} 

Now, when the cycle through all the details of the object "value", the class of which contains Prop "BackColor":

 foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value)) { foreach (Attribute attribute in property.Attributes) { ... } .... } 

the collection .Attributes property contains only one attribute of type "ConditionAttribute": the one who has "Test1". The rest are ignored ;-(

Does AllowMultiple not work for property attributes?

Thank you in advance

Henric

+5
attributes custom-attributes
May 11 '09 at 15:33
source share
3 answers

According to a post on MSDN , this is by design as part of the PropertyDescriptor class.

However, you can solve the problem by overriding TypeId in your custom attribute (thanks to Ivan from Mindscape for pointing this out):

 public override object TypeId { get { return this; } } 
+17
Sep 02 '09 at 3:11
source share
โ€” -

Yes it works. Not sure why it is not working through PropertyDescriptors.

You can always do: Attribute.GetCustomAttributes(methodInfo, typeof(ConditionAttribute))

0
May 11 '09 at 15:37
source share

Another way to set it up,

[ConditionAttribute ("Test1, Test2, Test3")] public Color BackColor {get; install;}

and in your validation code,

Dim lstProperties () As String = _ChkColors.Split (",") For each strProp as a string in lstPropertyes 'your check' return Next

0
Jan 24 '18 at 11:23
source share



All Articles