473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ReadOnlyAttribute on a class

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
Nov 15 '05 #1
7 3088
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]
- mv*@spam.guard.caspershouse.com

"Polo" <pb@rtech.be> wrote in message
news:3f**********************@news.skynet.be...
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

Nov 15 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> a écrit
dans le message de news:%2***************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Polo" <pb@rtech.be> wrote in message
news:3f**********************@news.skynet.be...
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


Nov 15 '05 #3
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" <pb@rtech.be> wrote in message
news:3f*********************@news.skynet.be...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> a écrit
dans le message de news:%2***************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Polo" <pb@rtech.be> wrote in message
news:3f**********************@news.skynet.be...
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



Nov 15 '05 #4
100
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2***************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Polo" <pb@rtech.be> wrote in message
news:3f**********************@news.skynet.be...
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


Nov 15 '05 #5
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";
}
}

"Polo" <pb@rtech.be> a écrit dans le message de
news:3f**********************@news.skynet.be...
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

Nov 15 '05 #6

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.

Nov 15 '05 #7
I use the solution proposed by Paul Bawin
Thank you

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> a écrit dans le message
de news:%2******************@cpmsftngxa07.phx.gbl...

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.

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Murat Tasan | last post by:
hi, i am having a small problem with constructing an inner class. i am using an inner class (and not a static nested class) because the methods of the inner class need access to the enclosing...
2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
11
by: John Allen | last post by:
Hi there, Given a "PropertyGrid" with one particular property that appears as follows: -Size Width 10 Height 20 where "Size" is the native .NET "Size" structure, if the property is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.