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:

Comments

  1. Anonymous9:23 AM

    This is completely on the money. It's especially important to note that if you're in reflector you have to make sure you've loaded the Silverlight assemblies. If you're not sure then look at the version number in the bottom left. 2.0.5.0 == Silverlight. Even mscorlib is different. Basic things such as Enum.GetNames are not present! Fun fun!

    ReplyDelete
  2. This is pretty much as trying to run two different versions of the CLR in a single application (although in the latest version or some coming version is possible?)

    but same as trying to reference 2.0 from 1.1 or viceversa... same concepts apply, silverlight is "just" another version (flavor) of the runtime

    ReplyDelete

Post a Comment

Popular posts from this blog

FIREFOX / IE Word-Wrap, Word-Break, TABLES FIX

Some observations on Script Callbacks, "AJAX", "ATLAS" "AHAB" and where it's all going.

FIX: Requested Registry Access is not allowed (Visual Studio 2008)