为什么将 DependencyProperty 成员声明为 public 而不是 protected?

发布于 2024-12-10 11:10:42 字数 471 浏览 1 评论 0原文

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

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

而不是那样:

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

当我们拥有 CLR“包装器”时,为什么我们需要从外部使用 DevProp 成员:

public bool Dep
{
    get { return (bool)GetValue(DepProperty); }
    set { SetValue(DepProperty, value); }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烧了回忆取暖 2024-12-17 11:10:42

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

依赖属性安全注意事项

依赖属性应声明为公共属性。依赖属性标识符字段应声明为公共静态字段。即使您尝试声明其他访问级别(例如 protected),也始终可以通过标识符结合属性系统 API 来访问依赖属性。由于元数据报告或值确定 API 是属性系统的一部分,例如 LocalValueEnumerator。有关详细信息,请参阅依赖属性安全性

无论如何,将它们公开为公共并没有任何害处,我认为。

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.

孤星 2024-12-17 11:10:42

依赖属性通常应被视为公共属性,可以由任何有权访问实例的调用者访问或至少可以发现。

我认为“依赖属性安全注意事项”部分
下面链接中的“可以帮助您了解为什么以这种方式实现/注册依赖项属性以及更多相关内容:

http://msdn.microsoft.com/en-us/library/ms753358.aspx

谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文