Reputation: 16003
This code will get us all the properties of a class:
Dim myPropertyInfo As PropertyInfo()
= myType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
or in C#:
PropertyInfo[] myPropertyInfo
= myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
But is there a way to get just the properties defined as ReadOnly?
Or, equally, to exclude the ReadOnly properties?
Upvotes: 4
Views: 745
Reputation: 754753
Just filter the results to those which have CanWrite
as False
Dim items As PropertyInfo() = Me. _
GetType(). _
GetProperties(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public). _
Where(Function(x) Not x.CanWrite). _
ToArray() _
Note the above code sample is assuming Visual Studio 2008
or higher and requires an import of System.Linq
. If you're using an older version you can do the following
Dim props As PropertyInfo() = Me.GetType().GetProperties(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)
Dim readOnlyProps As New List(Of PropertyInfo)
For Each cur in props
If Not cur.CanWrite Then
readOnlyProps.Add(cur)
End If
Next
Upvotes: 7