Why does WPF support binding to properties of an object, but not fields?

前端 未结 2 2102
感动是毒
感动是毒 2020-11-21 11:37

I\'ve got a WCF service that passes around status updates via a struct like so:

[DataContract]
public struct StatusInfo
{
    [DataMember] public int Total;
         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 11:57

    I can only guess why they only support properties: perhaps because it seems to be a universal convention in the .NET framework never to expose mutable fields (probably to safeguard binary compatibility), and they somehow expected all programmers to follow the same convention.

    Also, although fields and properties are accessed with the same syntax, data binding uses reflection, and (so I've heard) reflection must be used differently to access fields than to access properties.

提交回复
热议问题