如何指定等效的/features:strict(csc.exe的)到msbuild.exe或.csproj文件中? [英] How to specify the equivalent of /features:strict (of csc.exe) to msbuild.exe or in the .csproj file?

查看:134
本文介绍了如何指定等效的/features:strict(csc.exe的)到msbuild.exe或.csproj文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个简单(而且很糟糕)的C#类:

Consider this simple (and bad) C# class:

using System;

namespace N
{
  static class C
  {
    static void M(DateTime d)
    {
      if (d == null)
        Console.WriteLine("Yes");
      else
        Console.WriteLine("No");
    }

    static void L(object o)
    {
      if (o is Nullable)
        Console.WriteLine("Yes");
      else
        Console.WriteLine("No");
    }

  }
}

方法ML都存在严重问题.

Both methods M and L have serious issues.

M中,我们通过提升的==运算符(由于DateTime重载operator ==而存在)来询问不可为空的结构<​​c3>的值是否等于null.这总是很容易出错,编译器可以在编译时告知,因此我们有一个分支("Yes"),该分支无法访问.

In M, we ask if a value of the non-nullable struct DateTime is equal to null via the lifted == operator (which exists since DateTime overloads operator ==). This is always falls, and the compiler can tell at compile-time, so we have a branch ("Yes") which is unreachable.

N中,我们询问o是否是static class Nullable的实例,但永远不会这样(请注意,静态类Nullable与结构Nullable<>不同).同样,这是开发人员的错误,"Yes"语句不可访问.

In N we ask if o is an instance of the static class Nullable which can never be the case (note, the static class Nullable is not the same as the struct Nullable<>). Again, this is a developer mistake, and the "Yes" statement is unreachable.

在这些情况下,我们确实需要编译时警告(或警告为错误"),对吗?

We do want a compile-time warning (or "warning as error") in these cases, right?

看起来,通过逐步累积用于C#1.0到5.0的旧C#编译器中的编译器错误和/或遗漏,预期的编译时警告未能在旧编译器中出现.幸运的是,我们现在拥有Roslyn/C#6.0/Visual Studio 2015,希望得到警告.但是不,由于希望不从Roslyn发出警告,即旧编译器不存在该警告(向后兼容性?),因此仍然不对这些情况提出警告.

As it seems, through gradual accumulation of compiler errors and/or omissions in the old C# compiler that was used for C# 1.0 through 5.0, the expected compile-time warnings failed to appear with the old compiler. Luckily we have Roslyn/C# 6.0/Visual Studio 2015 now, and expect to get a warning. But no, because of the desire to not emit warnings from Roslyn that where not present with the old compiler (backwards compatibility?), these situations are still not warned against.

但是,如果使用csc.exe从命令行进行编译,则可以使用:

However, if you compile from the command line, with csc.exe, you can use:

csc.exe /features:strict ... ...

,您将收到想要的警告! /features:strict使csc.exe包含有关旧C#编译器忘记了"的警告.

and you will get the warnings you want! /features:strict makes csc.exe include warnings that the old C# compiler "fogot".

如何在.csproj文件中指定/features:strict等同于msbuild.exe命令行?

How do I specify the equivalent of /features:strict to msbuild.exe command line or in the .csproj file?

有时,例如当我们在构建项目中使用XAML时,直接使用csc.exe并不容易,我们必须使用.csproj文件并通过msbuild.exe进行编译.

Sometimes, e.g. when we have XAML in our build project, it is not easy to use csc.exe directly, we have to use a .csproj file and compile through msbuild.exe.

推荐答案

csproj文件直接支持此标志,只需添加:

This flag is supported in csproj file directly, just add:

<Features>strict</Features>

转到csproj文件中的相应PropertyGroup,并在构建后看到针对您的代码的以下警告:

To the appropriate PropertyGroup in your csproj file, and after build you will see this warning for your code:

警告CS8073表达式的结果始终为"false",因为类型为"DateTime"的值永远不会等于类型为"DateTime"的"null"?

Warning CS8073 The result of the expression is always 'false' since a value of type 'DateTime' is never equal to 'null' of type 'DateTime?'

如果要通过msbuild命令行界面执行相同的操作,只需使用/p:Features=strict设置此属性,如下所示:

If you want to do the same via msbuild command-line interface, just set this property with /p:Features=strict, like this:

/t:rebuild /p:Configuration=Debug /p:Platform=x64 /p:Features=strict

这篇关于如何指定等效的/features:strict(csc.exe的)到msbuild.exe或.csproj文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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