使用 TypeDescriptor 获取私有属性

发布于 2024-12-12 03:36:26 字数 359 浏览 0 评论 0原文

我想在 C# 中使用 TypeDescriptor 获取类的私有属性。

到目前为止,调用

TypeDescriptor.GetProperties(myType);

仅返回公共的非静态属性。

我还没有找到一种方法来影响 GetPropertiesGetProvider 方法以强制它们返回“默认”(公共、非静态)成员之外的成员。

请不要建议反射(我很清楚 BindingFlags),除非它给了我一个 PropertyDescriptor 对象。

I would like to get private properties of the class using TypeDescriptor in c#.

So far calling

TypeDescriptor.GetProperties(myType);

returns only public, non-static properties.

I have not found a way how to influence GetProperties or GetProvider methods to force them to return other than "default" (public, non-static) members.

Please do not suggest reflection (I am well aware of BindingFlags) unless it gives me a PropertyDescriptor object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱我好吗 2024-12-19 03:36:26

为此,您必须编写并注册一个确实使用反射的自定义TypeDescriptionProvider。但是,您当然可以这样做 - 您甚至可以拥有实际与字段(而不是属性)对话的 PropertyDescriptor 实例。您可能还需要编写自己的定制 PropertyDescriptor 实现,因为 ReflectPropertyDescriptor内部(您也许可以使用反射来获取它)。最终,您必须使用反射来实现,但您可以实现 TypeDescriptor.GetProperties(Type)< /code> 返回您想要的 PropertyDescriptor 实例。

您也可以对您无法控制的类型执行此操作。然而,应该强调的是,你的意图是不寻常的。

如果您使用 .GetProperties(instance) 重载,那么您还可以通过实现 ICustomTypeDescriptor 来实现此目的,它比完整的 TypeDescriptionProvider 更简单。

有关挂钩定制提供程序的示例,请参阅 HyperDescriptor

To do that you would have to write and register a custom TypeDescriptionProvider that does use reflection. You can certainly, however, do this - you can even have PropertyDescriptor instances that actually talk to fields (instead of properties). You will also probably need to write your own bespke PropertyDescriptor implementation since ReflectPropertyDescriptor is internal (you could perhaps use reflection to obtain that). Ultimately, you will have to use reflection for the implementation, but you can achieve the requirement that TypeDescriptor.GetProperties(Type) returns PropertyDescriptor instances that you want.

You can do this for types outside your control, too. It should be stressed, however, that your intent is unusual.

If you were using the .GetProperties(instance) overload, then you can also do this by implementing ICustomTypeDescriptor which is simpler than a full TypeDescriptionProvider.

For an example of hooking a bespoke provider, see HyperDescriptor

岁月静好 2024-12-19 03:36:26

您可以创建自己的 CustomPropertyDescriptor,它从 PropertyInfo 获取信息。

最近我需要获取非公共属性的PropertyDescriptorCollection

在我使用 type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic) 获取非公共属性后,我使用以下类创建相应的PropertyDescriptor。

class CustomPropertyDescriptor : PropertyDescriptor
{
    PropertyInfo propertyInfo;
    public CustomPropertyDescriptor(PropertyInfo propertyInfo)
        : base(propertyInfo.Name, Array.ConvertAll(propertyInfo.GetCustomAttributes(true), o => (Attribute)o))
    {
        this.propertyInfo = propertyInfo;
    }
    public override bool CanResetValue(object component)
    {
        return false;
    }

    public override Type ComponentType
    {
        get
        {
            return this.propertyInfo.DeclaringType;
        }
    }

    public override object GetValue(object component)
    {
        return this.propertyInfo.GetValue(component, null);
    }

    public override bool IsReadOnly
    {
        get
        {
            return !this.propertyInfo.CanWrite;
        }
    }

    public override Type PropertyType
    {
        get
        {
            return this.propertyInfo.PropertyType;
        }
    }

    public override void ResetValue(object component)
    {
    }

    public override void SetValue(object component, object value)
    {
        this.propertyInfo.SetValue(component, value, null);
    }

    public override bool ShouldSerializeValue(object component)
    {
        return false;
    }
}

You may create your own CustomPropertyDescriptor which gets the info from PropertyInfo.

Recently I need to get the PropertyDescriptorCollection of nonpublic properties.

After I used type.GetProperties(BindingFlags. Instance | BindingFlags.NonPublic) to get the nonpublic properties, then I use the following class to create the corresponding PropertyDescriptor.

class CustomPropertyDescriptor : PropertyDescriptor
{
    PropertyInfo propertyInfo;
    public CustomPropertyDescriptor(PropertyInfo propertyInfo)
        : base(propertyInfo.Name, Array.ConvertAll(propertyInfo.GetCustomAttributes(true), o => (Attribute)o))
    {
        this.propertyInfo = propertyInfo;
    }
    public override bool CanResetValue(object component)
    {
        return false;
    }

    public override Type ComponentType
    {
        get
        {
            return this.propertyInfo.DeclaringType;
        }
    }

    public override object GetValue(object component)
    {
        return this.propertyInfo.GetValue(component, null);
    }

    public override bool IsReadOnly
    {
        get
        {
            return !this.propertyInfo.CanWrite;
        }
    }

    public override Type PropertyType
    {
        get
        {
            return this.propertyInfo.PropertyType;
        }
    }

    public override void ResetValue(object component)
    {
    }

    public override void SetValue(object component, object value)
    {
        this.propertyInfo.SetValue(component, value, null);
    }

    public override bool ShouldSerializeValue(object component)
    {
        return false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文