简体   繁体   English

如何编写一个可以在.NET 3.5紧凑框架和常规框架上工作的类库?

[英]How to write a class library that will work on .NET 3.5 compact framework AND regular framework?

如何编写可以为常规.NET 3.5 Framework或.NET 3.5 Compact Framework重新编译的.NET类库?

The two frameworks aren't binary compatible*, unfortunately, but don't let that stop you. 遗憾的是,这两个框架不是二进制兼容*,但不要让它阻止你。

Create two projects in your solution (one normal Class Library project, and one Compact Framework Class Library project), and add all the files from one project to the other as links by clicking "Add|Existing File" and then checking the "add as link" checkbox on the file dialog. 在您的解决方案中创建两个项目(一个普通的类库项目和一个Compact Framework类库项目),并通过单击“添加|现有文件”然后选中“添加为”,将所有文件从一个项目添加到另一个项目作为链接链接“文件对话框上的复选框。

Now you only have one set of source code to maintain, but your solution will build both DLLs at the same time. 现在您只需要维护一组源代码,但您的解决方案将同时构建两个DLL。

If you have any code inside a file that's specific to the desktop framework and won't work on the compact framework, you can wrap it in a compiler directive (at least in C#) like this: 如果您在特定于桌面框架的文件中包含任何代码并且无法在紧凑框架上运行,则可以将其包装在编译器指令中(至少在C#中),如下所示:

#if PocketPC
    // mobile-specific stuff here
#else
    // desktop-specific stuff here
#endif
  • Note that although you can't use desktop-framework binaries on a mobile platform, the opposite is not true. 请注意,虽然您无法在移动平台上使用桌面框架二进制文件,但情况恰恰相反。 Compact framework executables can run on the desktop. 紧凑的框架可执行文件可以在桌面上运行。 I'm fairly certain, however, that a desktop application cannot reference a compact framework assembly (though I've never tried). 但是,我很确定桌面应用程序无法引用紧凑的框架程序集(尽管我从未尝试过)。

> The two frameworks aren't binary compatible > >这两个框架不是二进制兼容的>

Actually the Desktop version can load and run CF assemblies. 实际上,桌面版可以加载和运行CF程序集。

  1. Create a class library for the compact framework. 为紧凑框架创建一个类库。
  2. Add a reference to that library from your .exe project (Desktop or Mobile) 从.exe项目(桌面或移动)添加对该库的引用
  3. Profit! 利润!

Seriously, I don't know why the top answer is so top. 说真的,我不知道为什么最佳答案是如此顶级。 You don't need two separate projects at all. 您根本不需要两个单独的项目。 Also i'm not in love with pre-processor directives, they're ugly and require additional knowledge about the project when playing with the build parameters. 此外,我不喜欢预处理器指令,它们很难看,并且在使用构建参数时需要有关项目的其他知识。 It is much more pretty to outsource all the incompatible bits and pieces to an interface (IPlaformServices or such like) or you could even just ask: 将所有不兼容的零碎件外包给接口(IPlaformServices等)更加美观,或者您甚至可以问:

   if(Environment.OSVersion.Platform == PlatformID.WinCE)
   {
       // winCE specific     
   }
   else
   {
       // desktop specific
   }

Both of these are better solutions than preprocessor directives IMO. 这两个都是比预处理器指令IMO更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM