12/21/2008

Referencing a non-Silverlight Assembly in a Silverlight Project

“Bailout? Hey! I’m having trouble paying my bills. How about it?” -- Me

This one comes up a lot, and the bottom line is, you can’t do it.

But - I think it is important to understand why it won't work:

You cannot reuse non-Silverlight assemblies since the desktop CLR and Silverlight CLR are based on two different Frameworks. They are similar, but they aren’t “the same”. The BCL’s (Base Class Libraries) that are referenced in each type of project may have similar names but they are completely physically different files. But you can reuse the original code if it's compatible and available to you in source

code form.

In Visual Studio, you can create a new Silverlight class library project, then right click to add an existing item. You can click the arrow on the right side of the Add Button, and choose "Add As Link". This will not copy the .cs file to your new project, so when you modify the .cs file in one place, the other place will be automatically updated. You can also have two separate projects, one a full-framework one, and one a SIlverlight project, that both use the same class files. You can even save a Silverlight .csproj file right next to the full framework .csproj file (with a slightly different name) in order to promote code re-use.

As far as being able to "add a reference" to an assembly that wasn't compiled under a Silverlight project, you cannot do it. However, you can add a reference from a full .Net CLR to a Silverlight assembly.

Technorati Tags: