views:

213

answers:

5

I have this:

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

        }

And my utility class for databinding does this:

PropertyInfo pi = ReflectionHelper.GetPropertyInfo(boundObjectType, sourceProperty);

if (!pi.CanWrite)
                SetReadOnlyCharacteristics(boundEditor);

But PropertyInfo.CanWrite does not care whether the set is publicly accessible, only that it exists.

How can I determine if there's a public set, not just any set?

+1  A: 

You need to use the BindingFlags. Something like

PropertyInfo property = type.GetProperty("MyProperty", BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance);
Darren Kopp
A: 

Inside your ReflectionHelper.GetPropertyInfo(), you presumably to a boundObjectType.GetType().GetProperties(), where the BindingFlags parameter apparently includes BindingFlags.NonPublic. You want to specify just BindingFlags.Public

James Curran
This won't work, property can be public, while its "set" can be private or internal.
Ilya Ryzhenkov
+1  A: 

Call GetSetMethod on PropertyInfo, obtain MethodInfo and examine its properties, like IsPublic.

Ilya Ryzhenkov
+1  A: 

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

Jon Skeet
A: 

Well it's a little hard to tell since you have a "ReflectionHelper" class where we cannot see the source. However, my first guess is that you aren't properly setting the BindingFlags attribute when you call Type.GetProperty. You need to OR in the Public enumeration flag to ensure that only Public values are returned.

Nick