我正在绑定以收集放置在属性上的所有自定义属性。有多个相同类型的属性分配给属性,但在收集这些属性时,结果集合只包含特定类型的第一个属性:
属性类
[AttributeUsage(System.AttributeTargets.Property,
AllowMultiple = true)]
public class ConditionAttribute : Attribute{...}用法:
[ConditionAttribute("Test1")]
[ConditionAttribute("Test2")]
[ConditionAttribute("Test3")]
public Color BackColor{get; set;}现在,当循环遍历对象'value‘的所有道具时,它的类包含Prop "BackColor":
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value))
{
foreach (Attribute attribute in property.Attributes)
{ ... }
....
}集合property.Attributes只包含一个类型为"ConditionAttribute“的属性:带有"Test1”的属性。其他人则被忽略;
那么,AllowMultiple不适用于属性属性吗?
提前感谢
亨里克
发布于 2009-09-02 03:11:39
根据MSDN上的帖子,这是作为PropertyDescriptor类的一部分而设计的。
但是,实际上可以通过在自定义属性中重写TypeId来解决这个问题(感谢伊万来自心灵景观指出了这一点):
public override object TypeId
{
get
{
return this;
}
}发布于 2009-05-11 15:37:32
是的,确实有用。不知道为什么它不能通过PropertyDescriptors工作。
你总是可以做到的:Attribute.GetCustomAttributes(methodInfo, typeof(ConditionAttribute))
发布于 2018-01-24 11:23:00
另一种调整这个的方法,
ConditionAttribute("Test1,Test2,Test3")公共颜色BackColor{get;set;}
在你的验证代码中,
Dim lstProperties()作为String = _ChkColors.Split(",")作为lstPropertyes‘您的验证’中的字符串返回
https://stackoverflow.com/questions/848669
复制相似问题