Why create DependencyProperty member in this way:
public static readonly DependencyProperty DepProperty = DependencyProperty.Register(...);
and not in that way:
protected static readonly DependencyProperty DepProp = DependencyProperty.Register(...);
Why do we need to use the DevProp member from outside when we have the CLR "wrappers":
public bool Dep
{
get { return (bool)GetValue(DepProperty); }
set { SetValue(DepProperty, value); }
}
According to MSDN, restrictive access modifiers don't actually provide the intended access protection from certain APIs, so there's no point declaring dependency properties and their identifier fields anything other than public
:
Dependency Property Security Considerations
Dependency properties should be declared as public properties. Dependency property identifier fields should be declared as public static fields. Even if you attempt to declare other access levels (such as protected), a dependency property can always be accessed through the identifier in combination with the property system APIs. Even a protected identifier field is potentially accessible because of metadata reporting or value determination APIs that are part of the property system, such as LocalValueEnumerator. For more information, see Dependency Property Security.
It doesn't do any harm exposing them as public
anyway, I'd gather.
Dependency properties should generally be considered to be public properties, accessible or at least discoverable by any caller that has access to an instance.
I think the section "Dependency Property Security Considerations " in below link can help you understand why dependency properties are implemented/registered in this way and more on this:
http://msdn.microsoft.com/en-us/library/ms753358.aspx
Thanks
来源:https://stackoverflow.com/questions/7818476/why-declare-dependencyproperty-members-public-not-protected