简体   繁体   English

DependencyObject.SetValue 与 cast 和 set 属性

[英]DependencyObject.SetValue vs cast and set property

I have a property changed callback and in it i need to perform some validation.我有一个属性更改回调,我需要在其中执行一些验证。

I am going to take a new value and validate it against a set of other property criteria such as min and max values.我将采用一个新值并根据一组其他属性标准(例如最小值和最大值)对其进行验证。

To do this I am planning to either take the dependency object from the changed event and use为此,我计划从已更改的事件中获取依赖项对象并使用

DependencyObject.SetValue(TargetProperty,NewValue);

or cast it to a variable and use the properties directly或将其强制转换为变量并直接使用属性

ObjectType myObjectType = (ObjectType)DependencyObject;
myObjectType.Target=NewValue;

My question is what would be the reasons for using either method over the other.我的问题是使用任何一种方法而不是另一种方法的原因是什么。 Would casting be more of a drain on resources than say lots of SetValue/GetValue lookups etc?与说大量的 SetValue/GetValue 查找等相比,转换会更消耗资源吗? I will be referencing properties up to 10 times in the methods.我将在方法中引用属性最多 10 次。

Many thanks.非常感谢。

The standard implementation of a dependency property in a DependencyObject is to call SetValue() . DependencyObject依赖属性的标准实现是调用SetValue() Furthermore, casting is dirt cheap, especially in this context.此外,铸造非常便宜,尤其是在这种情况下。 So there's really no practical difference between the two approaches.所以这两种方法之间真的没有实际区别。

For me, it would come down to what part of the operation you want to focus on.对我来说,这将归结为您想要关注的操作部分。 If you want your implementation tied closely to the fact that the object is in fact a DependencyObject , I can't see much reason to avoid calling SetValue() directly.如果您希望您的实现与对象实际上是DependencyObject的事实紧密DependencyObject ,我看不出有太多理由避免直接调用SetValue() That's what WPF does.这就是 WPF 所做的。

On the other hand, if you want the code to be more C#-like and follow normal property accessor idioms, casting to the correct type and then setting the property directly will be more readable and maintainable.另一方面,如果您希望代码更像 C# 并遵循正常的属性访问器习惯用法,则转换为正确的类型然后直接设置属性将更具可读性和可维护性。 Yes, it adds the extra operations of casting and then calling the property setter.是的,它添加了强制转换然后调用属性设置器的额外操作。 But those are negligible costs, practically immeasurable.但这些都是微不足道的成本,几乎无法估量。 And in return, you get code that doesn't assume any particular implementation of the property.作为回报,您将获得不假设该属性的任何特定实现的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只能在 DependencyObject 的 DependencyProperty 上设置绑定 - 当属性被 new 覆盖时 - Binding can only be set on a DependencyProperty of a DependencyObject - when property is overridden with new 如何在PropertyChangedCallback中将DependencyObject转换为FileInfo - How to cast DependencyObject as FileInfo in PropertyChangedCallback 不能在“ MiniListView”类型的“ Headers”属性上设置“ Binding”。 只能在DependencyObject的DependencyProperty上设置“绑定” - A 'Binding' cannot be set on the 'Headers' property of type 'MiniListView'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject 如何为从dependencyobject派生的类型的依赖属性设置默认值 - How can i set a default value for a dependency property of type derived from dependencyobject 依赖项属性的SetValue需要一个对象:如何设置FontSizeProperty? - SetValue for Dependency Property requires an object: How do I set the FontSizeProperty? 使用FieldInfo.SetValue与LINQ表达式在结构中设置字段 - Using FieldInfo.SetValue vs LINQ expressions to set a field in a struct 只能在 DependencyObject 的 DependencyProperty 上设置“A 'Binding”。 DependencyObject 上的错误 - Getting a 'A 'Binding' can only be set on a DependencyProperty of a DependencyObject.' error on a DependencyObject '[Unknown]'属性不指向路径'(0)中的DependencyObject。(1)[3]。(2)' - '[Unknown]' property does not point to a DependencyObject in path '(0).(1)[3].(2)' 反射属性.SetValue( - Reflection Property.SetValue( 'Foreground' 属性不指向路径 '(Foreground).(0)' 中的 DependencyObject - 'Foreground' property does not point to a DependencyObject in path '(Foreground).(0)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM