在什么情况下将字段更改为属性是一个突破性的变化? [英] Changing fields to property is a breaking change under what scenarios?

查看:22
本文介绍了在什么情况下将字段更改为属性是一个突破性的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 Jon Skeet 关于字段与属性的文章时,他提到将字段更改为属性是一个突破性的变化.

While reading Jon Skeet's article on fields vs properties he mentions that changing fields to properties is a breaking change.

我想了解此更改可能导致中断的常见情况.与场景一起,如果可以,请提供任何详细信息.

I would like to understand the common scenarios in which this change can cause breaks. Along with the scenario, if you can, please provide any details.

首先,以下几点已在别处提及:

For starters, the following points have been mentioned elsewhere:

  • 如果您在类上使用反射,则不能将字段更改为属性.即使我没有详细信息,这也是显而易见的.序列化是一种使用反射来迭代对象并将字段更改为属性将破坏序列化程序或更改输出的场景

  • You can't change fields to properties if you are using reflection on the class. This is obvious even though I don't have details. Serialization is one scenario where reflection is used to iterate over the object and changing fields to properties will break the serializer or change the output

您无法轻松绑定字段.(这是为什么?我在此处阅读)

You can't easily bind against fields. (Why is this? I read it here)

???

罗伯特有一份选择的完整原因列表字段上的属性,并解释了它们之间的切换如何导致重大更改.

Robert has a comprehensive list of reasons for choosing properties over fields and also explains how switching between them can cause a breaking change.

推荐答案

属性可以抛出任意异常,而字段不能(至少当编译器在编译时知道字段分配时).

Properties can throw any arbitrary exceptions, whereas fields can't (at least when compiler knows about field assignment at compile time).

这篇关于在什么情况下将字段更改为属性是一个突破性的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆