我应该如何为不同版本的 .NET 编写具有独特部分的代码
我的源代码需要支持 .NET 版本 1.1 和 2.0 ...我如何测试不同的版本和版本? 处理这种情况的最佳方法是什么?
我想知道是否应该将两部分代码内联在单独的类、方法等中。您觉得怎么样?
My source code needs to support both .NET version 1.1 and 2.0 ... how do I test for the different versions & what is the best way to deal with this situation.
I'm wondering if I should have the two sections of code inline, in separate classes, methods etc. What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们遇到了这个问题,最终得到了一个“兼容层”,我们在其中为 .NET v1.1 和 v2.0 实现了一组接口和实用程序代码。
然后我们的安装人员为正确的版本制定了正确的代码。 我们使用了 NSIS(免费!),它们具有您可以调用来确定 .NET 版本的函数。
We had this problem and we ended up with a "compatability layer" where we implemented a single set of interfaces and utility code for .NET v1.1 and v2.0.
Then our installer laid down the right code for the right version. We used NSIS (free!), and they have functions you can call to determine the .NET version.
如果您想做这样的事情,您将需要使用预处理器命令和条件编译符号。
我会使用明确指示您所针对的 .NET 版本的符号(例如 NET11 和 NET20),然后像这样包装相关代码:
这样做的原因而不是简单的 if/else 是一个额外的保护层以防有人忘记定义符号。
话虽如此,您应该真正深入了解您想要/需要这样做的原因的核心。
If you want to do something like this you will need to use preprocessor commands and conditional compilation symbols.
I would use symbols that clearly indicate the version of .NET you are targeting (say NET11 and NET20) and then wrap the relevant code like this:
The reason for doing it this way rather than a simple if/else is an extra layer of protection in case someone forgets to define the symbol.
That being said, you should really drill down to the heart of the reason why you want/need to do this.
我会问为什么你必须维护两个代码库的问题,如果有机会的话我会选择一个并使用它。
尝试使两个代码库与更改数量和更改类型保持同步将非常复杂,并且为任一版本构建的构建过程也将非常复杂。
I would be asking the question of WHY you have to maintain two code bases, I would pick one and go with it if there is any chance of it.
Trying to keep two code bases in sync with the number of changes, and types of changes would be very complex, and a build process to build for either version would be very complex.
这里有很多不同的选择。 在我工作的地方,我们使用#if pragmas,但也可以使用不同版本的单独程序集来完成。
理想情况下,您至少应将版本相关代码保留在单独的部分类文件中,并在编译时提供正确的版本。 如果我能回到过去,我会强制执行此操作,我们的代码库现在有大量 #if 编译指示,有时可能很难管理。 整个 #if pragma 事情中最糟糕的部分是 Visual Studio 会忽略任何无法使用当前定义进行编译的内容,因此很容易检查重大更改。
NUnit 支持 1.1 和 2.0,因此是测试框架的不错选择。 使用 NAnt 之类的东西来制作单独的 1.1 和 2.0 版本,然后自动运行 NUnit 测试并不太难。
There are a lot of different options here. Where I work we use #if pragmas but it could also be done with separate assemblies for the separate versions.
Ideally you would at least keep the version dependant code in separate partial class files and make the correct version available at compile time. I would enforce this if I could go back in time, our code base now has a whole lot of #if pragmas and sometimes it can be hard to manage. The worst part of the whole #if pragma thing is that Visual Studio just ignores anything that won't compile with the current defines and so it's very easy to check in breaking changes.
NUnit supports both 1.1 and 2.0 and so is a good choice for a test framework. It's not too hard to use something like NAnt to make separate 1.1 and 2.0 builds and then automatically run the NUnit tests.