自动属性问题 [英] Problems with auto-properties

查看:70
本文介绍了自动属性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是反射器给出的:

public int Int1 { get; set; }
public string StringA { get; set; }

// Fields
[CompilerGenerated]
private int <Int1>k__BackingField;
[CompilerGenerated]
private string <StringA>k__BackingField;

问题是C#规范没有明确指定如何命名自动属性的后备字段。
这可能是二进制序列化的问题(从C#3.0的 Nutshell中获取)。
自动属性还有其他问题吗? MS应该在C#规范中指定此名称吗?

The problem is that C# specification doesn't explicitly specify how backing fields for automatic properties are named. This can be a problem for binary serialization (from book C# 3.0 in a Nutshell). Are there any other problems with auto-properties ? Should MS specify this in C# specs ?

推荐答案

我不认为MS应该指定名称-我认为这样做更合理避免二进制序列化,在我看来,二进制序列化总是有些脆弱。 (Java序列化同样也很脆弱。)如果它依赖于字段名(并且可能有一种通过归因于属性的方法来解决这个问题-我对二进制序列化还没有足够的经验),那么它已经相当讨厌地限制了实现更改

I don't think MS should specify a name - I think it's more reasonable to avoid binary serialization, which is always going to be somewhat brittle in my opinion. (Java serialization is similarly brittle.) If it relies on field names (and there may be a way of getting round that by attributing your properties - I'm not sufficiently experienced with binary serialization to know) then it's restricting implementation changes quite nastily already.

我对自动属性的最大问题是无法创建真正只读属性(具有只读后备字段)。不过,我之前对此一直大声疾呼...

My biggest problem with automatic properties is that there's no way of creating genuinely readonly properties (with readonly backing fields). I've ranted about this before though...

这篇关于自动属性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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