且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF / Silverlight / XAML中的强类型数据绑定?

更新时间:2023-02-25 19:14:53

Ken,C#将从引用PropertyInfo类的简洁语法元素中受益PropertyInfo结构是在编译时定义的静态对象,因此提供了一个独特的关键在于对象上的每个属性,然后可以在编译时验证属性。

Ken, C# would benefit from a concise syntactical element for referencing a PropertyInfo class. PropertyInfo structures are static objects defined at compile time and as such provide a unique key for every Property on an object. Properties could then be validated at compile time.

唯一的问题是将对象的实例视为数据类型的奇怪性,因为强类型是强制类型的,而不是类型的值。传统上,编译器不执行数据值,而是依赖于运行时代码来检查其数据。大多数情况下,它甚至不可能在编译时验证数据,但反射是其中至少可能的边缘情况之一。

The only problem with this is the weirdness of treating an instance of a object as a data type given that strong typing is enforced on types, not the values of a type. Traditionally, compilers don't enforce data values, and instead rely on runtime code to check its data. Most cases its not even possible to validate data at compile time, but reflection is one of those edge cases where it is at least possible.

或者,编译器可以为每个属性创建一个新的数据类型。我可以想象很多类型被创建,但这将使编译时强制的属性绑定。

Alternatively, the compiler could create a new data type for every property. I could imagine a lot of types being created, but that would enable compile time enforcement of property binding.

考虑这一点的一个方法是CLR引入了与之前的系统相比的另一个重要程度。它现在被用来做一些令人印象深刻的东西,如数据绑定。但是它的实现仍然在元数据级别,从编译器为每种数据类型生成一种报告。我应该有一种方式来增长C#将是促进元数据来编译时间检查。

One way to think about it is that the CLR introduced a level of reflection that was another level of magnitude compared to the systems that preceded it. It's now being used to do some rather impressive stuff like data binding. But its implementation is still at a metadata level, a kind of report from the compiler for every data type is generates. I supposed one way to grow C# would be to promote the metadata to compile time checking.

在我看来,有人可以开发一个编译工具来增加反射级验证。新的智慧是这样的。一般来说,发现要与PropertyInfos进行比较的字符串参数是非常棘手的,但这不是不可能的。可以定义一个新的数据类型,如PropertyString,可以清楚地识别将来与PropertyInfos进行比较的参数。

It seems to me that someone could develop a compilation tool that adds that reflection level validation. The new intellisense is sorta like that. It would be tricky to generically discover string parameters that are destined to be compared to PropertyInfos, but its not impossible. A new data type like "PropertyString" could be defined that clearly identifies parameters that will be compared to PropertyInfos in the future.

无论如何,我感到你的痛苦。我追逐了很多拼错的物业名称参考。老实说,WPF中有很多与反射有关的刺激。一个有用的实用程序将是一个WPF执行检查器,可以确保所有的静态控制构造函数都到位,属性被正确定义,绑定是准确的,正确的键等。可以执行很长的验证列表。

Anyway, I feel your pain. I've chased down a lot of misspelled property name references. Honestly, there are a lot of irritations in WPF related to reflection. A useful utility would be a WPF enforcement checker that makes sure all your static control constructors are in place, your attributes properly defined, bindings are accurate, correct keys, etc. There is a long list of validations that could be performed.

如果我还在为Microsoft工作,我可能会尝试这样做。

If I were still working for Microsoft, I'd probably try doing it.