如何强制使用特定版本的 .net 框架
我们的印象是,在解决方案的属性上设置目标框架会限制该应用程序仅使用该框架或以下的功能。但我们刚刚发现,有人可以添加引用并开始使用来自更高版本框架的代码,并且编译器不会抱怨一点。因为我们希望将来防止这种情况发生,所以有人对我如何检测引用更高版本的内容有任何想法吗?如果有人在我们的目标之上添加代码,我需要使构建失败。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的目标是 .NET 2.0 及更高版本...如果您在项目的引用中检测到对 System.Core 或其他 3.x 程序集(例如 WPF)的引用,您的构建可能会失败。
更新
您可以首先检查每个
.PROJ
文件中的:然后,在
标记内:这可能是自定义 NAnt任务或编写自己的解析器来查找这些节点并使构建失败。
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.
UPDATE
You could start with checking inside each
.PROJ
file for:Then, inside the
<ItemGroup>
tag:This could be a custom NAnt task or write your own parser to look for these nodes and fail the build.
如果它有效,为什么需要阻止它?只要您不使用任何新功能,我相信就有可能拥有“向前”兼容的 DLL。
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.