views:

315

answers:

3

How do I write a .NET class library that I can recompile for either the regular .NET 3.5 Framework, or the .NET 3.5 Compact Framework?

+7  A: 

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.

Now you only have one set of source code to maintain, but your solution will build both DLLs at the same time.

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:

#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).
Matt Hamilton
I've been successfully running desktop applikations - and ASP.Net sites - that reference compact framework assemblies for quite some time now. Seems like there is no problem with that what so ever...
Johan Kullbom
Good to know. Thanks Johan.
Matt Hamilton
Actually ... does that mean you have to install the Compact Framework on your target machines? Or do NETCF assemblies use the desktop framework if it's available?
Matt Hamilton
No, Compact Framework does not need to be installed on the target machines. So the desktop assemblies must be used. As I remember it there was a quite comprehensive document on MSDN about this issue but I can't find it now...
Johan Kullbom
Yes it just runs under the desktop edition of CLR. Therefore it is worth knowing about the differences between FF and CF if you're going to be sharing code between the two. I have a very incomplete list of these here: http://stackoverflow.com/questions/342576/do-you-know-any-run-time-differences-between-compact-and-full-framework-code
Quibblesome
+3  A: 

> The two frameworks aren't binary compatible >

Actually the Desktop version can load and run CF assemblies.

logicnp
Ah yes that's true. +1 and I will amend my response.
Matt Hamilton
+2  A: 
  1. Create a class library for the compact framework.
  2. Add a reference to that library from your .exe project (Desktop or Mobile)
  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:

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

Both of these are better solutions than preprocessor directives IMO.

Quibblesome
Preprocessor directives mean that the code doesn't end up in the assembly if it's not required (or supported) on that platform. That's important on the compact framework where you want to keep your assemblies as small as possible.
Matt Hamilton
Fair point. If disk space and memory are pressing concerns then use just one preprocessor command to generate your IPlatformServices. Having them littered throughout a code base can be hellish though so I don't recommend that route.
Quibblesome