For those of you that use the Silverlight tools for Visual Studio, you have probably tried adding a reference from a Silverlight project to another non-Silverlight project in the same solution, just to see what happens. If do so, you will be met with the following message:
Or if you try to do it the other way round:
Why is this? Well, Silverlight uses different CLR to the rest of .NET, known as the CoreCLR. The CoreCLR is a stripped down version containing only the stuff deemed necessary by the Silverlight team. Also, if you look at the version number of any of the Silverlight assemblies (e.g System.Xml) you will see that the version is 2.0.5 whilst the version of System.Xml in a non-Silverlight assembly is 2.0.0. They are not binary compatible.
So imagine the situation where you have a class library which you wish to build for Silverlight consumption. There are two solutions:
1. Create a new Silverlight class library, and add all the class files to the project. This is fine for a small project.
2. Edit the .csproj file manually, and convert it to a Silverlight project. This is good if you have a large project with lots of file and subdirectories.
I originally looked into this because I wanted to convert to the Ninject.Core, a lightweight dependency injection framework (www.ninject.org), to Silverlight. It wasn't too hard - here are the steps to do so:
- Add the following lines in the first <PropertyGroup> section:
<ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>- In the DefineConstants section, add a define for SILVERLIGHT and clauses to specify not to build against standard libraries, e.g
<SilverlightApplication>false</SilverlightApplication>
<DefineConstants>TRACE;DEBUG;SILVERLIGHT</DefineConstants>- Change <Reference include="System"> to <Reference include="system">
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
- Add the following lines right at the end (just before the </project> tag)
<ProjectExtensions>Hope this helps
<VisualStudio>
<FlavorProperties GUID="{A1591282-1198-4647-A2B1-27E5FF5F6F3B}">
<SilverlightProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
Neil