【问题标题】:Filter properties returned by TypeDescriptor.GetProperties() depending on the class they are declared in根据声明的类过滤 TypeDescriptor.GetProperties() 返回的属性
【发布时间】:2009-10-11 22:00:03
【问题描述】:

大家好。我有以下情况。

我想使用 TypeDescriptor 来获取某个类型的属性。继承层次结构中类型的深度可能会有所不同。我只想获取在类型本身而不是在其父级(基)中声明的属性。问题是,当我调用 TypeDescriptor.GetProperties() 时,它会将继承层次结构中声明的所有内容返回到 Object。

我只看到我可以按属性过滤输出,但我不想仅仅为此向我的类型中的属性添加另一个属性。让它们通过反射而不使用 TypeDescriptor 会做我想要的,但对我来说不是一个选择,因为某些属性会在某个时候动态添加到类型中。

有什么想法吗?如果问题不清楚,我可以举个例子。

【问题讨论】:

    标签: c# .net reflection typedescriptor


    【解决方案1】:

    您可以使用ComponentType 属性过滤属性:

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

    【讨论】:

      【解决方案2】:

      你不能只修改ICustomTypeDescriptor 的实现以反映你想要的行为吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-05
        • 2012-08-29
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多