Binary compatibility between different .NET platforms? RRS feed

  • Question

  • If I have a library that only contains some math algorithems, could the same DLL used in different .NET platforms without re-compile the code for different platforms?

    For example, I build a class library(DLL) that only contains some mathematical operations and I build it for .NET Framework 2.0 in visual studio. Then, could the same DLL be used in Silverlight 2.0 and .NET Compact Framework 2.0 without recompile the code?


    Lei Jiang

    Lei Jiang
    Thursday, January 8, 2009 1:00 PM

All replies

  • If you build a Compact Framework Class Library (DLL), it'll work on a desktop full fat .NET application, not sure about Silverlight though, I've never used it.
    Thursday, January 8, 2009 3:03 PM
  • I think SL and full framweowrk dlls are binary compatible but there are lot of things to be considered before trying to run an assembly built against full fx on Silverlight. This is because, they are not source compatible. SL is a subset of full fx, and if you are using APIs/interfaces not present in SL, you will see errors. Silverlight also has a different security model, and your code may run fine on full fx but can get security exceptions on SL. SL apps run in a hosted environment which also makes a difference. Please read the below article for more details.


    Thursday, January 8, 2009 9:02 PM
  • Thanks!

    My code is just mathematical computation, no OS-specific code, no device-specific code, etc. From your reply, it seems I only need to compile it in .NET Framework 2.0, and copy the DLL into Silverlight applicatoin, or windows mobile device and it should work? Has anyone ever tried it? Maybe I should try it on my machine.

    Lei Jiang

    Lei Jiang
    Friday, January 9, 2009 3:49 AM
  • However, every Silverlight DLL has a reference to "mscorlib" which is not present in a .NET Framework DLL. How to resolve it?
    Lei Jiang
    Friday, January 9, 2009 3:56 AM
  • It most certainly is.  It's the "mother" assembly, containing the basics like System::String etc.
    Hans Passant.
    Friday, January 9, 2009 4:01 AM
  • msorlib.dll is one of the core assemblies and part of both SL and full framework. However, as I mentioned previously, SL mscorlib.dll has a much reduced surface area than full fx mscorlib. From what I understand, even though your library has math algorithms you may be using some APIs which are not a part of SL. You need to verify that for your math library.
    Friday, January 9, 2009 6:15 AM
  • Another problem is of the signature of system DLLs, in Silverlight 2.0, the assembly name of mscorlib.dll is:

    mscorlib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e

    However, in the full fx, the assmebly name of mscorlib.dll is:

    mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089

    This means that DLL compiled in one platform could not be used in another since the required reference is different in manifest. Is this true? It seems I have to recompile the source code for another target platform to make it works on it.

    Lei Jiang
    Friday, January 9, 2009 10:08 AM
  • Ok so heres the catch. If your library only has reference to mscorlib, you should be good. This is because mscorlib.dll is treated in a special way by the runtime in many ways. The runtime unifies the right mscorlib always. If your library has references to framework assemblies other than mscorlib, it wont be binary compatible between full fx and SL. Ildasm your assembly to check the refrences. Also, as I suggested earlier, please do the right testing and verification. This is all a bit complicated and there are several rules and exclusions to be aware of. And the best way to know that your app will be compatible across all frameworks is to test it on all of them. The article I posted earlier should be helpful too.

    Friday, January 9, 2009 7:34 PM
  • So it's impossible to keep binary compatibility for my DLL since it will use other assemblies such as System.DLL.

    So I have to build a set of assembly output for each platform with the same source code. Is there any easy way to do this in Visual Studio 2008? The way I am currently using seems stupid: I create a project for each platform, in one of them add all source code files into it, and in other projects, I add the same source code as a link (in "Add existing items..."dialog, add a file with "Add as Link"). Then compile each project to get output assemblies for each platform. When I add or remove files in one project, I have to do the same for other projects, it's inconvenient. When I test the projects, it's more troublesome: I have to configure three set of test projects.....Is there any simple way to build and test software for different target platforms (they are all .net frameworks!)?
    Lei Jiang
    Saturday, January 10, 2009 11:39 AM
  • It's interesting that dll & exe built for .NET Compact Framework can run from within windows explorer directly.
    Lei Jiang
    Monday, January 12, 2009 7:43 AM