C#6.0 /只读自动属性/不变量 [英] C# 6.0 / Read Only Auto-Properties / Invariants

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

问题描述

我认为与C#6.0相关的仪器/静态分析器中存在很少的错误,并且只读自动属性:

是否有计划添加新语法?

I think there are few bugs in the instrumentation / static analysier related to C# 6.0 and read only auto properties:
Is there any plans to add the new syntax?

以下是一个例子:

Here is an example:

public WebHeaderCollection Header { get;  } = new WebHeaderCollection();

似乎无法检测到这是一个只读的自动属性,并且具有可证明的不变量 

It seems unable to detect that this is a read only auto property and would have a provable invariant of 

Contract.Invariant(this.Header != null);

那里是一个明显的解决方法:不要使用新语法

There is an obvious work around: don't use the new syntax

public ClassCtor() 
{	
    this.Header = new WebHeaderCollection();
}

public WebHeaderCollection Header { get; private set; } 

推荐答案

详细说明一点点,即使你把不变量放在契约不变量方法中,静态分析器也会抱怨。  目前,似乎没有办法让代码合同与新的只读自动属性一起使用。  我是
猜测它们生成的字节码与旧样式生成的字节码不匹配,因此静态分析器不会注意到它是一个自动属性。
To elaborate on this a little bit, even if you put the invariant in a contract invariant method the static analyzer complains.  At the moment, it appears that there is no way to make Code Contracts work with the new readonly auto properties.  I'm guessing the generated bytecode for them doesn't match the old style generated bytecode and so the static analyzer doesn't notice it is an auto-property.


这篇关于C#6.0 /只读自动属性/不变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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