Tuesday, March 17, 2009

Convert dotnet assemblies to Silverlight

The SilverLighter
I have recently been trying to convert an object database (db40 and NeoDatis) for use in a Silverlight application. Along the way I discovered a few interesting things. For example I found that although Visual Studio will not allow you to reference dotnet assemblies from a Silverlight application or class library, this restriction is actually a bit heavy handed. Sometimes it is very useful to use a known Silverlight-safe dotnet assembly, as long as you take responsibility for your own actions.

To enable the re-use of dotnet assemblies in Silverlight I wrote a handy WPF application called  "The Silverlighter". This tool allows you  to convert any dotnet assembly into a Silverlight assembly ready for use in your Silverlight applications or class libraries. 

The knowledge I needed to write The Silverlighter was gleaned primarily from the excellent article by David Betz called Reusing .NET Assemblies in Silverlight. This article clearly explains how the dotnet->Silverlight conversion process works and why it is not a hack. I thought this article was a fascinating insight into the similarities between the dotnet and Silverlight assemblies and is well worth a read.

But before you get carried away (I know I did) and imagine that you are just a click away from re-using your favourite 3rd party assemblies, a word or two of caution. 

Unfortunately just because you can reference an assembly from Silverlight does not mean it will work with Silverlight. If you have read David Betz' article you will know that Silverlight uses a distinct set of System.* assemblies (v 2.0.5.0). These assemblies do not contain all the features of their equivalent dotnet assemblies. For example the following collection types are missing in Silverlight:
  • ArrayList
  • Hashtable
  • SortedList
  • NameValueCollection
Instead Silverlight only allows generic collections to be used, which is a good thing unless your referenced ex-dotnet assembly happens to use one or more of the missing types, in which case your code will blow up with an error similar to "cannot load type ArrayList from assembly System 2.0.5.0"

Replacing these missing collections with the generic equivalents (you can decompile most assembly code using Reflector) is actually quite trivial however more serious problems are lurking. Silverlight is also missing core features such as:
You can see that Silverlight has removed or redesigned features that are security risks. Again that is a good thing, until you try to re-use assemblies that use these features - then boom.

Anyway, The Silverlighter app does some cool IL trickery so if you have dotnet dlls you know are fine and just want to use them in Silverlight without any nonsense from Visual Studio then it might be just the thing you need.

Additional Notes

There are a few options available to help tweak the functionality.


You can choose to convert only selected System assemblies, although it is recommended that you just leave them all selected unless you have a good reason not to.

The "Recursively process dependent assemblies" feature will, when checked, pick out references from the IL to non System assemblies (your own or 3rd party references) and recursively convert these to Silverlight compliant assemblies as well. The entire dependency tree will be processed in this way.

Finally the path to the ILdasm.exe is exposed just in case you have it at a different location on your system. If you don't have ILdasm.exe anywhere you can get by installing .NET Framework 2.0 Software Development Kit (SDK)

If you notice any bugs or want to add new features then please feel free to checkout the source code and make the updates. Just paste the SVN url into your TortoiseSVN repo-browser, check out the source code under subversion and you are off and running. Good luck.

SVN URL = http://subversion.assembla.com/svn/biofractal/trunk/Blog/Silverlighter


14 comments:

  1. Anonymous7:49 am

    This is a fantastic tool, many many thanks!

    ReplyDelete
  2. Thanks. I hope you can make use of it. Remember, if you want to make it better then feel free :-)

    ReplyDelete
  3. Thanks a lot for this app, saved me the hassle of writing it! (I found this via your comment at the bottom of the excellent David Betz article). Good effort posting up the source too.

    I've noticed that if I use the installer from the link above it creates the "Silverlight" subdir but not the assembly. If I grab the SVN and run it all works fine and the assembly is generated.

    I might add command line support for this so I can add it to my build actions. If I do I'll let you know!

    ReplyDelete
  4. Josh

    If you add the command line feature then just commit it to Assembla and drop me a line. Thanks for your interest.

    ReplyDelete
  5. This is great, does Silverlight 3 reduce the number of unsupported features? For example, Silverlight 3 has a binary serialization feature, doesn't it? It would be great if this were updated for Silverlight 3 ;)

    Thanks,

    ReplyDelete
  6. Anonymous7:55 pm

    This approach is very similar to Suriel Bendahan's SLAsm
    (http://www.codeproject.com/KB/silverlight/SLAssemblies.aspx)
    Although the latter allows you to customize the process and strip problematic Attributes via config

    ReplyDelete
  7. Nice tool.
    However, in Visual Studio 10 compiled DLL's it only replaces the resources, not the DLL itself. Probably Microsoft changed ILDASM, or the DLL structure itself? Or is the converter lokking fotr specific strings that now have changed?

    Paul

    ReplyDelete
  8. Anonymous2:16 pm

    Where can I download you app, am getting a 404 error with your link.

    ReplyDelete
  9. Links are broken...

    ReplyDelete
  10. Sorry about broken links - they are fixed now

    ReplyDelete
  11. Anonymous5:25 pm

    Cool application. Thank you very much!

    ReplyDelete
  12. Anonymous5:47 pm

    I tried this on Microsoft.PointofService.dll and it did not create a new dll in the silverlight folder where it put the resources

    ReplyDelete
  13. Anonymous10:32 am

    I tried to convert my dll which I created using class library project but it does not work.
    It says that successfully converted. (creates folder silverlight but that folder is empty).

    ReplyDelete
  14. Anonymous5:54 am

    Any luck on getting the PointOfService.dll to work. I am having the same problem

    ReplyDelete