Cross compiling for .NET 4 and Silverlight 4 in VS2010 without duplicating files

We have a large number of projects within the solution, mainly simple class libraries (which are later loaded via MEF) designed for .NET 4.0.

We would like to compile a large number of such files for both .NET 4.0 and Silverlight without duplicating files.

Is there a way to create a new Silverlight class library and link the source files to other projects so that the .NET 4.0 library and Silverlight 4.0 library are compiled?

I know that .NET 4.0 can load silverlight 4 assemblies, but I would like to compile both versions anyway, rather than compiling everything for Silverlight.

Update: I saw a solution when some of the projects contained links to other files in other projects, so when you changed the file in one project, it was also updated in another. This is what I mean.

alt text http://i31.tinypic.com/33esow4.png

Screenshot of the solution, the Vialis.Led.Interfaces project contains the source files, in the silverlight project I want to create links to these files.

+4
source share
5 answers

Just set up a second project for Silverlight, and then use Project->Add Existing Files... to add each of the project files to your Silverlight project.

You can also use partial classes to highlight Silverlight or .NET-specific functions. (This is the approach used by Prism, by the way.)

+4
source

If you want to reuse the code, you have basically three options:

  • As a rule, create all the base class libraries as a Silverlight class library project, as this is the structure with the lowest feature set. Throw away all the links except mscorlib.dll , System.dll and System.Core.dll . You can then link such a Silverlight library in any complete .NET project.

  • You can link individual code files to another project using the "Add as link" function (right-click on the project → Add existing item → Change the "Add" button to "Add as link"). This way you can create a Silverlight project and link the individual files to your complete .NET project. However, this can become tedious if you have many files, and you often add / delete files and folders in the original project.

  • To fix this problem, you can check the project linker at http://msdn.microsoft.com/en-us/library/dd458870.aspx ... but I haven't tried it myself yet.

+7
source

Microsoft recently released the Portable Library Tools: http://msdn.microsoft.com/en-us/library/gg597391.aspx

I will definitely look at them.

+3
source

Yes. Create two projects in one directory, one with Silverlight, one regular.

You can also automate this, the .csproj file is based on Xml. Removing, especially the following, will make it regular:

 <ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids> 
0
source

As stated in the comments in the accepted answer, you use the arrow in the "Add" button in the file dialog box to do the "Add as link".

To add one more small thing that may be useful: it is important to remember that Silverlight and .NET 4.0 do not match. If you have code that compiles in one but not the other, dev tools by default define the SILVERLIGHT compilation symbol for Silverlight, so you can do #if SILVERLIGHT.

0
source

All Articles