C# 获取按自定义属性筛选的PropertyDescriptorCollection
我需要获得一个C# 获取按自定义属性筛选的PropertyDescriptorCollection,c#,custom-attributes,typedescriptor,propertydescriptor,C#,Custom Attributes,Typedescriptor,Propertydescriptor,我需要获得一个PropertyDescriptorCollection,其中包含用自定义属性修饰的所有属性。问题是TypeDescriptor.GetProperties只能通过精确匹配所有属性的属性进行过滤,因此,如果我想获得所有属性,无论属性的属性如何设置,我都必须覆盖过滤器数组中的所有可能性 以下是我的属性代码: [AttributeUsage(AttributeTargets.Property)] class FirstAttribute : Attribute { public
PropertyDescriptorCollection
,其中包含用自定义属性修饰的所有属性。问题是TypeDescriptor.GetProperties
只能通过精确匹配所有属性的属性进行过滤,因此,如果我想获得所有属性,无论属性的属性如何设置,我都必须覆盖过滤器数组中的所有可能性
以下是我的属性代码:
[AttributeUsage(AttributeTargets.Property)]
class FirstAttribute : Attribute
{
public bool SomeFlag { get; set; }
}
以及具有装饰属性的类:
class Foo
{
[First]
public string SomeString { get; set; }
[First(SomeFlag = true)]
public int SomeInt { get; set; }
}
主要内容:
static void Main(string[] args)
{
var firstPropCollection = TypeDescriptor.GetProperties(typeof(Foo), new Attribute[] {new FirstAttribute()});
Console.WriteLine("First attempt: Filtering by passing an array with a new FirstAttribute");
foreach (PropertyDescriptor propertyDescriptor in firstPropCollection)
{
Console.WriteLine(propertyDescriptor.Name);
}
Console.WriteLine();
Console.WriteLine("Second attempt: Filtering by passing an an array with a new FirstAttribute with object initialization");
var secondPropCollection = TypeDescriptor.GetProperties(typeof(Foo), new Attribute[] { new FirstAttribute {SomeFlag = true} });
foreach (PropertyDescriptor propertyDescriptor in secondPropCollection)
{
Console.WriteLine(propertyDescriptor.Name);
}
Console.WriteLine();
Console.WriteLine("Third attempt: I am quite ugly =( ... but I work!");
var thirdCollection = TypeDescriptor.GetProperties(typeof(Foo)).Cast<PropertyDescriptor>()
.Where(p => p.Attributes.Cast<Attribute>().Any(a => a.GetType() == typeof(FirstAttribute)));
foreach (PropertyDescriptor propertyDescriptor in thirdCollection)
{
Console.WriteLine(propertyDescriptor.Name);
}
Console.ReadLine();
}
static void Main(字符串[]args)
{
var firstPropCollection=TypeDescriptor.GetProperties(typeof(Foo),新属性[]{new FirstAttribute()});
WriteLine(“第一次尝试:通过传递带有新FirstAttribute的数组进行过滤”);
foreach(firstPropCollection中的PropertyDescriptor PropertyDescriptor)
{
Console.WriteLine(propertyDescriptor.Name);
}
Console.WriteLine();
WriteLine(“第二次尝试:通过在对象初始化时传递具有新FirstAttribute的数组进行过滤”);
var secondPropCollection=TypeDescriptor.GetProperties(typeof(Foo),新属性[]{new FirstAttribute{SomeFlag=true}});
foreach(secondPropCollection中的PropertyDescriptor PropertyDescriptor)
{
Console.WriteLine(propertyDescriptor.Name);
}
Console.WriteLine();
WriteLine(“第三次尝试:我很丑=(…但我工作!”);
var thirdCollection=TypeDescriptor.GetProperties(typeof(Foo)).Cast()
.Where(p=>p.Attributes.Cast
正如您所看到的,我只能在最后一次尝试中获得这两个属性。我不确定我是否理解这个问题……您想要所有具有特定属性的属性,而不考虑此属性值吗
class Program
{
static void Main(string[] args)
{
var props = typeof(Foo).GetProperties();
var filtered = props
.Where(x => x.GetCustomAttributes(typeof(FirstAttribute), false).Length > 0)
.ToList();
var propertyDescriptor = TypeDescriptor.GetProperties(typeof(Foo))
.Find(filtered[0].Name, false);
}
}
class Foo
{
[First]
public string SomeString { get; set; }
[First(SomeFlag = true)]
public int SomeInt { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
class FirstAttribute : Attribute
{
public bool SomeFlag { get; set; }
}
您的解决方案的问题是,您正在使用PropertyInfo[]检索信息
。我需要使用PropertyDescriptorCollection,因为我确实需要PropertyDescriptor为每个与条件匹配的属性创建PropertyDescriptor。Type.GetProperties检索一个PropertyInfo数组。您是对的。编辑了我的帖子。您可以从代码中迭代的集合中获取匹配的描述符。但这取决于您调用此函数的频率你应该对这两个函数进行基准测试,哪一个函数做得更好…谢谢。我想我可以做到。事实上,我正在寻找一个只涉及TypeDescriptor的解决方案,但还没有。