Why declare DependencyProperty members public not protected?

守給你的承諾、 提交于 2019-12-01 05:33:54

问题


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); }
}

回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!