的AllowMultiple不与属性的属性工作? [英] AllowMultiple does not work with Property Attributes?

查看:1039
本文介绍了的AllowMultiple不与属性的属性工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑收集所有定义属性放置在属性。有分配给属性相同类型的多个属性,但收集它们时,所得到的集合只包含特定类型的第一属性

I'm tying to collect all Custom Attributes placed over a Property. There are more than one Attributes of the same type assigned to the Property, but when collecting them , the resulting collection only contains the first Attribute of the specific type:

属性类

[AttributeUsage(System.AttributeTargets.Property,
               AllowMultiple = true)]

public class ConditionAttribute : Attribute{...}

用法:

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

现在通过对象的价值,它的类包含了道具背景色的所有道具循环时:

Now when looping through all Props of the object 'value' whose class contains the Prop "BackColor":

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

集合的 property.Attributes 只包含一个属性的类型ConditionAttribute:一个与测试1。 ,而忽略其他;-(

the collection property.Attributes only contains ONE Attribute of type "ConditionAttribute" : The one with "Test1". The others are ignored;-(

因此​​,没有的AllowMultiple不行物业属性?

So does AllowMultiple not work for Property Attributes ?

在此先感谢

亨里克

推荐答案

据<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/e6bb4146-eb1a-4c1b-a5b1-f3528d8a7864\">a张贴在MSDN ,这是设计为的PropertyDescriptor类的一部分。

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

不过,你其实可以通过在自定义属性覆盖TYPEID解决问题(感谢伊万从思维空间中指出这一点):

However, you can actually 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;
  }
}

这篇关于的AllowMultiple不与属性的属性工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆