Discussion:
How to determine read/write status of a property
chris Burgess
2008-02-25 16:02:53 UTC
Permalink
How do I discover if a property is read only or read/write at runtime
(other than trying to write to it)?

e.g.
If:
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property

then I want the textbox on the form to be read only.

If:
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

then I want the textbox to be read/write.

Thanks,

Chris

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Brady Kelly
2008-02-25 16:19:35 UTC
Permalink
Post by chris Burgess
How do I discover if a property is read only or read/write at runtime
(other than trying to write to it)?
Get a PropertyInfo object using Reflection and check its CanWrite property.
To get the PropertyInfo, use
PropertyOwner.GetType().GetProperty("PropertyName").

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Efran Cobisi
2008-02-25 16:17:54 UTC
Permalink
Hi Chris,

You need to query the property's PropertyInfo class for its CanRead and
CanWrite boolean properties.

Type t = ...;
PropertyInfo pi = t.GetProperty("Name");

if (pi.CanRead)
...

if (pi.CanWrite)
...

HTH

--
Efran Cobisi
http://www.cobisi.com
Post by chris Burgess
How do I discover if a property is read only or read/write at runtime
(other than trying to write to it)?
e.g.
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
then I want the textbox on the form to be read only.
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
then I want the textbox to be read/write.
Thanks,
Chris
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Loading...