Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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);
        }
    }

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!!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top