通过这取决于他们在声明的类TypeDescriptor.GetProperties()返回的过滤性能声明、性能、TypeDescriptor、GetProperties

2023-09-06 08:35:40 作者:岁月之沉淀

嘿,伙计们。我有以下的情况。

Hey guys. I have the following situation.

我想使用TypeDescriptor得到某种类型的属性。在继承层次类型的深度可能会有所不同。我只是想在类型本身而不是其父母(基地)声明的属性。问题是,当我叫TypeDescriptor.GetProperties()将返回所有宣布了继承层次多达对象。

I want to use a TypeDescriptor to get the properties of a certain type. The type's depth in the inheritance hierarchy may vary. I only want to get the properties declared in the type itself and not in its parents (base). The problem is that when I call TypeDescriptor.GetProperties() it would return everything declared up the inheritance hierarchy up to Object.

我只看到我可以过滤由属性的输出,但我不希望添加另一个属性的属性在我的类型只是为了这个。让他们通过反射和不使用TypeDescriptor会做我想做的,但不是我的选择,因为某些属性将被动态添加到类型在一些点。

I only saw that I can filter the output by Attributes, but I don't want to add another attribute to the properties in my types just for this. Getting them through reflection and not using TypeDescriptor would do what I want, but is not an option for me, because some of the properties will be added dynamically to the type at some point.

任何想法?如果这个问题不明确,我可以提供一个例子。

Any ideas? If the question is not clear I could provide an example.

推荐答案

您可以使用 COMPONENTTYPE 属性筛选器属性:

You can filter the properties using the ComponentType property :

var properties = from p in TypeDescriptor.GetProperties(x).Cast<PropertyDescriptor>()
                 where p.ComponentType == x.GetType()
                 select p;