通过这取决于他们在声明的类TypeDescriptor.GetProperties()返回的过滤性能 [英] Filter properties returned by TypeDescriptor.GetProperties() depending on the class they are declared in

查看:295
本文介绍了通过这取决于他们在声明的类TypeDescriptor.GetProperties()返回的过滤性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

这篇关于通过这取决于他们在声明的类TypeDescriptor.GetProperties()返回的过滤性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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