为什么宣布DependencyProperty成员公开不受保护? [英] Why declare DependencyProperty members public not protected?

查看:90
本文介绍了为什么宣布DependencyProperty成员公开不受保护?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以这种方式创建DependencyProperty成员:

Why create DependencyProperty member in this way:

public static readonly DependencyProperty DepProperty = DependencyProperty.Register(...);

而不是这样:

protected static readonly DependencyProperty DepProp = DependencyProperty.Register(...);

为什么我们需要CLR包装器时需要从外部使用DevProp成员: / p>

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


推荐答案

根据MSDN ,限制性访问修饰符实际上并未提供针对某些API的预期访问保护,因此,除了 public 之外,没有任何必要声明依赖项属性及其标识符字段:

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:


依赖项属性安全注意事项


依赖项属性应声明为公共属性。依赖项属性标识符字段应声明为公共静态字段。即使您尝试声明其他访问级别(例如,受保护的),也始终可以通过标识符结合属性系统API来访问依赖项属性。由于元数据报告或属性系统中包含的值确定API,例如 LocalValueEnumerator 。有关更多信息,请参见依赖项财产安全

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.

无论如何,我将它们公开为 public 并没有任何危害。

It doesn't do any harm exposing them as public anyway, I'd gather.

这篇关于为什么宣布DependencyProperty成员公开不受保护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆