为什么将 DependencyProperty 成员声明为 public 而不是 protected?
为什么以这种方式创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 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
:It doesn't do any harm exposing them as
public
anyway, I'd gather.依赖属性通常应被视为公共属性,可以由任何有权访问实例的调用者访问或至少可以发现。
我认为“依赖属性安全注意事项”部分
下面链接中的“可以帮助您了解为什么以这种方式实现/注册依赖项属性以及更多相关内容:
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