473,387 Members | 1,420 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,387 software developers and data experts.

Properties of a Property

Is it possible to detect if a property of a class is Readonly, WriteOnly, or
Read/Write?
Feb 16 '06 #1
3 883
Is it possible to detect if a property of a class is Readonly, WriteOnly, or
Read/Write?


With reflection, check the PropertyInfo.CanRead and CanWrite
properties.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 16 '06 #2
Ok, so I took your advice and looked into reflection, and here's a quick and
dirty of what I came up with:

Private Sub frmObject_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.Text = CType(Me.Tag, CommandOperationBase).GetType.ToString

Dim a As Assembly = Assembly.GetAssembly(Me.Tag.GetType)
Dim s As String = String.Empty

For Each t As Type In a.GetExportedTypes
For Each mi As MemberInfo In t.GetMembers
If mi.DeclaringType Is Me.Tag.GetType Then
If mi.MemberType = MemberTypes.Property Then
s &= mi.Name & vbCrLf
End If
End If
Next
Next

MessageBox.Show(s)

End Sub

Basically, I created a new instance of frmObject from another form, set its
Tag property equal to an object, then load the form. This code just
displays all of the properties of that object.

But therein lies the problem. I don't just need the properties of that
object, but also of the object it inherits (all the way back to the base
object) as well. How can I change the code to do this?

Thanks again,

Jason

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ue**************@TK2MSFTNGP11.phx.gbl...
Is it possible to detect if a property of a class is Readonly, WriteOnly,
or
Read/Write?


With reflection, check the PropertyInfo.CanRead and CanWrite
properties.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 17 '06 #3
> For Each t As Type In a.GetExportedTypes
For Each mi As MemberInfo In t.GetMembers
If mi.DeclaringType Is Me.Tag.GetType Then
If mi.MemberType = MemberTypes.Property Then
s &= mi.Name & vbCrLf
End If
End If
Next
Next
You're loading a lot of unneccesary metadata here for types and
members that you're not interested in. A simpler way would be

For Each mi As MemberInfo In Me.Tag.GetType().GetProperties()
s &= mi.Name & vbCrLf
Next

But therein lies the problem. I don't just need the properties of that
object, but also of the object it inherits (all the way back to the base
object) as well. How can I change the code to do this?


You should see them as well. GetProperties (and GetMethods) by default
return all the properties in the inheritance hierarchy, unless you
specify BindingFlags.DeclaredOnly.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 18 '06 #4

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

Similar topics

24
by: downwitch | last post by:
Hi, I know this has been covered here and in the .public groups, but it seems like it's been a while, especially around here, so I just thought I'd ask again to see if anyone has figured out a...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
3
by: John Baro | last post by:
I have a class as: public class a : { private float m_b; public float b { get {
3
by: Martin Montgomery | last post by:
I have, for example, a property called myProperty. I would like, when using a property grid to display the property name as "My Property". Is this possible. Is there an attribute etc Thank ...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
47
by: Jon Slaughter | last post by:
private string name; public string Name { get { return name; } set { name = value; } } In the above, why doesn't C# just allow one to create a single directive to make a property?
39
by: Paul Mcilreavy | last post by:
Hi, i would like to get some view points on the use of properties within classes. My belief is that properties should generally be used to return values of private members. They should not do...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.