ReadOnlyAttribute on a class

  • Thread starter Thread starter Polo
  • Start date Start date
P

Polo

Hi All,

I would like to apply dynamically the ReadOnlyAttribute(True - False) on a
class to lock or not the edition of instances of it in the property grid
An other solution is welcome.

Syncerely
Polo
 
Polo,

In order to do this you will have to pass in a type that implements
ICustomTypeDescriptor. Once you have that, you will have to return the
properties with the appropriate attributes attached through the
GetProperties method. However, if you want to change the read-only status
while the property grid is attached to an object, you will probably have to
set the SelectedObject property again to rebind the property grid to the
type. The property grid probably assumes (reasonably) that you are not
going to change the type and therefore caches on the initial binding what is
read only and what is not.

Hope this helps.
 
Thank 's Nicholas but how can I change the Attribute ReadOnly dynamically ?

bool custom ;
AttributeCollection attributes = TypeDescriptor.GetAttributes(this, custom);
ReadOnlyAttribute readonly =
(ReadOnlyAttribute)attributes[typeof(ReadOnlyAttribute)];
readonly .IsReadOnly = true; <----------------- I can not make it


Nicholas Paldino said:
Polo,

In order to do this you will have to pass in a type that implements
ICustomTypeDescriptor. Once you have that, you will have to return the
properties with the appropriate attributes attached through the
GetProperties method. However, if you want to change the read-only status
while the property grid is attached to an object, you will probably have to
set the SelectedObject property again to rebind the property grid to the
type. The property grid probably assumes (reasonably) that you are not
going to change the type and therefore caches on the initial binding what is
read only and what is not.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Polo said:
Hi All,

I would like to apply dynamically the ReadOnlyAttribute(True - False) on a
class to lock or not the edition of instances of it in the property grid
An other solution is welcome.

Syncerely
Polo
 
Hmm... that's exactly what Nicholas was saying.

Look up TypeDescriptor on MSDN. You will have to implement your own type
descriptor for the type you're interested. There is a overridable method
"GetProperties" which will be called whenever your type's properties are
enumerated. You will have to override it and pass out a
PropertyDescriptorCollection. You will be able to modify the attributes at
that point.

-vJ

Polo said:
Thank 's Nicholas but how can I change the Attribute ReadOnly dynamically ?

bool custom ;
AttributeCollection attributes = TypeDescriptor.GetAttributes(this, custom);
ReadOnlyAttribute readonly =
(ReadOnlyAttribute)attributes[typeof(ReadOnlyAttribute)];
readonly .IsReadOnly = true; <----------------- I can not make it


Nicholas Paldino said:
Polo,

In order to do this you will have to pass in a type that implements
ICustomTypeDescriptor. Once you have that, you will have to return the
properties with the appropriate attributes attached through the
GetProperties method. However, if you want to change the read-only status
while the property grid is attached to an object, you will probably have to
set the SelectedObject property again to rebind the property grid to the
type. The property grid probably assumes (reasonably) that you are not
going to change the type and therefore caches on the initial binding
what
is
read only and what is not.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Polo said:
Hi All,

I would like to apply dynamically the ReadOnlyAttribute(True - False)
on
 
In addition to what Nicholas said I want to add that it is not possible to
dynamically apply or remove custom attributes.
Custom attributes are objects serialized in the assembly meta data. They are
created at compile time and reside in the assembly image file. Thus they are
kind of readonly.

B\rgds
100

Nicholas Paldino said:
Polo,

In order to do this you will have to pass in a type that implements
ICustomTypeDescriptor. Once you have that, you will have to return the
properties with the appropriate attributes attached through the
GetProperties method. However, if you want to change the read-only status
while the property grid is attached to an object, you will probably have to
set the SelectedObject property again to rebind the property grid to the
type. The property grid probably assumes (reasonably) that you are not
going to change the type and therefore caches on the initial binding what is
read only and what is not.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Polo said:
Hi All,

I would like to apply dynamically the ReadOnlyAttribute(True - False) on a
class to lock or not the edition of instances of it in the property grid
An other solution is welcome.

Syncerely
Polo
 
I circumvented the problem by this system :

When the object must be locked , I pass at the SelectedObject (in the
PropertyGrid) an object ObjectLocked that has a reference to the object
otherwise I pass directly the object

[ReadOnly(true)
public class ObjectLocked
{
private readonly object _Reference;

public ObjectLocked(object o)
{
_Reference = o;
}

[TypeConverter(typeof(LockedObjectConverter))]
public object Object
{
get { return _Reference; }
}
}
internal class LockedObjectConverter : ExpandableObjectConverter
{
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
return "This object is locked";
}
}
 
Hi Polo,

Have you found out how to get this done?
I have asked other colleague of mine to help you.
Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top