问题描述
考虑一下这个简单(但很糟糕)的 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");
}
}
}
M
和 L
两种方法都有严重的问题.
Both methods M
and L
have serious issues.
在 M
中,我们通过提升的 ==
运算符 (自从 DateTime
重载 operator ==
后就存在.这总是falls,编译器可以在编译时判断,所以我们有一个无法访问的分支(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# 编译器fogot"的警告.
and you will get the warnings you want! /features:strict
makes csc.exe
include warnings that the old C# compiler "fogot".
如何在 中指定
文件?/features:strict
到 msbuild.exe
命令行或 的等效项.csproj
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文件中直接支持这个flag,只需添加:
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 表达式的结果始终为假",因为日期时间"类型的值永远不会等于日期时间"类型的空"?
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
这篇关于如何在 msbuild.exe 或 .csproj 文件中指定/features:strict (of csc.exe) 的等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!