ReadOnlyAttribute 与 PropertyDescriptor.IsReadOnly()

发布于 2024-07-16 23:32:43 字数 141 浏览 5 评论 0原文

使用为 IsReadOnly() 方法返回值的 PropertyDescriptor 和与 ReadOnlyAttribute 关联的 PropertyDescriptor 之间有什么区别?

What is the difference between using a PropertyDescriptor that returns a value for the IsReadOnly() method, and one that is associated with a ReadOnlyAttribute?

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

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

发布评论

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

评论(3

灯角 2024-07-23 23:32:43

主要区别在于,如果您提供自己的 PropertyDescriptor 实现(通过 ICustomTypeDescriptorTypeDescriptionProviderTypeConverter),这允许您获得更多控制权)。 然后,您可以选择自己的逻辑(例如,基于访问权限)何时可写。

但是,是的; 在默认实现下,它将报告没有 setter 的属性以及标记有 ReadOnlyAttribute 的属性为只读。

The main difference is that this allows you to seize more control if you provide your own PropertyDescriptor implementation (via ICustomTypeDescriptor, TypeDescriptionProvider or TypeConverter). Then you can choose your own logic for when it is writeable - for example, based on access rights.

But yes; under the default implementation, it will report read-only for properties without setters, and for properties marked with ReadOnlyAttribute.

小嗲 2024-07-23 23:32:43

当我使用 Reflector 查看时没有区别。

派生类之一 SimplePropertyDescriptor 有以下代码。


    public override bool IsReadOnly
    {
        get
        {
            return this.Attributes.Contains(ReadOnlyAttribute.Yes);
        }
    }

No difference when I look at it using Reflector.

One of the derived class SimplePropertyDescriptor has the following code.


    public override bool IsReadOnly
    {
        get
        {
            return this.Attributes.Contains(ReadOnlyAttribute.Yes);
        }
    }

变身佩奇 2024-07-23 23:32:43

只是一个注释。

我花了一天时间为应用程序中的实体对象实现 ICustomTypeDescriptor,以便单独控制每个实体的只读状态。

因此,每个 PropertyDescriptor 实现都保留对其来源的实体对象的引用,因此 IsReadOnly 属性类似于以下内容:

public override bool IsReadOnly
{
    get { return _owner.IsReadOnly;}
}

但是,当我运行代码时,BindingSource 组件从 ICustomTypeDescriptor 的 GetProperties() 方法读取一组 PropertyDescriptor然而,集合中的每条记录,当它检查 IsReadOnly 的值时,它只测试从第一条记录获取的 PropertyDescriptor。

完全浪费时间!!!!

Just a Note.

I spent a day implementing ICustomTypeDescriptor for the entity objects in my application, in order to control the read-only state of each entity individually.

Thus, each PropertyDescriptor implementation kept a reference to the entity object it came from, so the IsReadOnly property was something like this:

public override bool IsReadOnly
{
    get { return _owner.IsReadOnly;}
}

However, when I ran the code the BindingSource component read a set of PropertyDescriptor s from the GetProperties() method of ICustomTypeDescriptor for each record in the set, however, when it checked the value of IsReadOnly, it only tested the PropertyDescriptor obtained from the first record.

Complete waste of time!!!!

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