如何强制执行特定版本的.net框架

时间:2021-11-20 23:02:00

We were under the impression that setting the target framework on the properties of a solution would limit that app to using only that framework or below's functionality. We just found out though, that someone can add a reference and start using code from a higher versioned framework and the compiler won't complain one bit. Since we would like to prevent this in the future does anyone have any ideas on how I could detect something referencing a higher version or not? I need to fail the build if someone adds code above our target.

我们的印象是,在解决方案的属性上设置目标框架会限制该应用程序仅使用该框架或以下的功能。我们刚刚发现,有人可以添加引用并开始使用更高版本的框架中的代码,编译器不会抱怨一点。既然我们希望将来阻止这种情况,那么有没有人对如何检测引用更高版本的内容有任何想法?如果有人在我们的目标之上添加代码,我需要使构建失败。

2 个解决方案

#1


1  

Assuming you're targeting .NET 2.0 and above... your build could fail if you detect references to System.Core or other 3.x assemblies (e.g. WPF) in References of your projects.

假设您的目标是.NET 2.0及更高版本...如果在项目的引用中检测到对System.Core或其他3.x程序集(例如WPF)的引用,则构建可能会失败。

UPDATE

You could start with checking inside each .PROJ file for:

您可以从每个.PROJ文件中检查:

<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>

Then, inside the <ItemGroup> tag:

然后,在 标记内:

<Reference Include="System.Core">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>

This could be a custom NAnt task or write your own parser to look for these nodes and fail the build.

这可能是一个自定义的NAnt任务,或者编写自己的解析器来查找这些节点并使构建失败。

#2


0  

Why do you need to prevent this if it works? As long as you don't use any of the new features, I believe it is possible to have "forward" compatible DLLs.

如果它有效,你为什么需要防止它?只要您不使用任何新功能,我相信可以拥有“前向”兼容的DLL。

#1


1  

Assuming you're targeting .NET 2.0 and above... your build could fail if you detect references to System.Core or other 3.x assemblies (e.g. WPF) in References of your projects.

假设您的目标是.NET 2.0及更高版本...如果在项目的引用中检测到对System.Core或其他3.x程序集(例如WPF)的引用,则构建可能会失败。

UPDATE

You could start with checking inside each .PROJ file for:

您可以从每个.PROJ文件中检查:

<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>

Then, inside the <ItemGroup> tag:

然后,在 标记内:

<Reference Include="System.Core">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>

This could be a custom NAnt task or write your own parser to look for these nodes and fail the build.

这可能是一个自定义的NAnt任务,或者编写自己的解析器来查找这些节点并使构建失败。

#2


0  

Why do you need to prevent this if it works? As long as you don't use any of the new features, I believe it is possible to have "forward" compatible DLLs.

如果它有效,你为什么需要防止它?只要您不使用任何新功能,我相信可以拥有“前向”兼容的DLL。