How to determine if a class property has a public collection (.NET)?

I have it:

public string Log { get { return log; } protected set { if (log != value) { MarkModified(PropertyNames.Log, log); log = value; } } } 

And my data binding utility class does this:

 PropertyInfo pi = ReflectionHelper.GetPropertyInfo(boundObjectType, sourceProperty); if (!pi.CanWrite) SetReadOnlyCharacteristics(boundEditor); 

But PropertyInfo.CanWrite does not care about whether the collection is publicly available, only that it exists.

How to determine if there is a public , and not just any ?

+6
reflection c # data-binding
source share
5 answers

An alternative to the proposed ReflectionHelper changes in other answers is to call pi.GetSetMethod(false) and see if the result is null.

+1
source share

You need to use BindingFlags . Something like

 PropertyInfo property = type.GetProperty("MyProperty", BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance); 
+2
source share

Call GetSetMethod in PropertyInfo, get MethodInfo, and examine its properties, such as IsPublic.

+1
source share

Inside your ReflectionHelper.GetPropertyInfo (), you are supposedly using boundObjectType.GetType (). GetProperties (), where the BindingFlags parameter seems to include BindingFlags.NonPublic. You want to specify only BindingFlags.Public

0
source share

Well, it's a little hard to say, since you have a ReflectionHelper class where we do not see the source. However, first of all, I assume that you set the BindingFlags attribute incorrectly when you call Type.GetProperty. You need an OR in the Public enumeration element to ensure that only public values ​​are returned.

0
source share

All Articles