Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

c# - 如果在解决方案文件中的任何项目中设置了某些MsBuild属性,是否可以引发自定义编译错误?(Is there a way to throw a custom compilation error if some MsBuild property is set in any project within a solution file?)

Background I have created a Directory.Build.props in my root repo folder with the following contents:

(背景信息我在根repo文件夹中创建了Directory.Build.props ,其内容如下:)

<Project>
  <PropertyGroup>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
</Project>

I need to prevent the usage of C# 8 syntax within our codebase, since it is targeting .Net 4.7.2 The above file sets the LangVersion property for each project within the solution.

(我需要防止在我们的代码库中使用C#8语法,因为它的目标是.Net 4.7.2。上面的文件为解决方案中的每个项目设置LangVersion属性。)

The problem

(问题)

Our codebase is big.

(我们的代码库很大。)

Many projects within one single solution with many programmers working in differents and / or adding new ones, this is not a problem itself but rather the fact that any programmer can override the LangVersion within their own .csproj file.

(在一个解决方案中的许多项目中,有许多程序员从事不同的工作和/或添加新的程序员,这本身不是问题,而是任何程序员都可以在自己的.csproj文件中覆盖LangVersion的事实。)

I know we can stop those changes in the code review phase or by sending reminders every hour to all the programmers telling them not to use C# 8 for this specific project.

(我知道我们可以在代码审查阶段停止这些更改,也可以每小时向所有程序员发送提醒,告诉他们不要为该特定项目使用C#8,从而停止这些更改。)

But I was wondering if I could just celebrate their boldness by giving them a nice and handsome compiler error.

(但是我想知道是否可以通过给他们一个漂亮而英俊的编译器错误来庆祝它们的大胆。)

Maybe with a custom code analizer with Roslyn?

(也许使用Roslyn的自定义代码分析器?)

Is there any way?

(有什么办法吗?)

  ask by taquion translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try something like this:

(尝试这样的事情:)

<Project>

    <Target Name="RestrictLangVersion" BeforeTargets="Compile">
        <PropertyGroup>
            <UnsupportedTarget Condition=" '$(MaxSupportedLangVersion)' == '7.3' AND '$(LangVersion)' == '8.0' ">true</UnsupportedTarget>
        </PropertyGroup>
        <Error Text="At *YOUR_COMPANY* we discourage the use of C# 8.0 on unsupported targets" Condition=" '$(UnsupportedTarget)' == 'true' " />
    </Target>

</Project>

You can pop that in a Directory.Build.targets high up in your CI agent and it should do the trick.

(您可以在CI代理中将它弹出到Directory.Build.targets中,它应该可以解决问题。)

You'll need to add the logic for preview etc... (as there are other values of LangVersion that will result in 8.0 ) and some of those will be dependant on the version of csc , so I've left out the hard part.

(您需要为preview等添加逻辑...(因为LangVersion其他值将导致8.0 ),其中一些值取决于csc的版本,所以LangVersion去了困难的部分。)

Basically you'll need to replicate the logic here: https://github.com/dotnet/roslyn/blob/97123b393c3a5a91cc798b329db0d7fc38634784/src/Compilers/CSharp/Portable/LanguageVersion.cs#L353-L364

(基本上,您需要在此处复制逻辑: https : //github.com/dotnet/roslyn/blob/97123b393c3a5a91cc798b329db0d7fc38634784/src/Compilers/CSharp/Portable/LanguageVersion.cs#L353-L364)

bearing in mind that it will change across different versions of csc .

(请记住,它将在不同版本的csc发生变化。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...