views:

214

answers:

4

We have a large number of projects within a solution mostly simple class libraries (which are later loaded through MEF) targetting .NET 4.0.

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

Is there a way to create a new Silverlight class library and link the source files from the other projects so both the .NET 4.0 library and the Silverlight 4.0 library will be compiled?

I'm aware .NET 4.0 can load silverlight 4 assemblies, but I would like to compile both versions anyway instead of compiling everything for Silverlight.

Update: I saw a solution once where some of the projects contained links to other files in other projects, so when you changed a file in one project it would be updated in the other one as well. This is what I mean.

alt text

A screenshot of the solution, the Vialis.Led.Interfaces project contains the original files, in the silverlight project I want to create links to these files.

+4  A: 

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

You can also use partial classes to separate out Silverlight-specific or .NET framework specific functionality. (This is the approach used by Prism, btw.)

Reed Copsey
But this makes a copy of the files right?
TimothyP
@TimothyP: No. If you do add existing item, and keep the projects in the same folder, they use the exact same files... No copies required. :)
Reed Copsey
I see, let me give that a try
TimothyP
nope it copies it to the silverlight project folder... any key I need to hold?
TimothyP
@TimothyP: You need to create your silverlight project **in the same folder** (on disk) as the .NET project. Just have them next to each other in the directory structure, and duplicate the project structures...
Reed Copsey
@TimothyP: It'll show two entries in the Solution Explorer - but when you edit the file, it's editing the same file on disk, and the changes recompile in both projects...
Reed Copsey
Right click the little down arrow on the right side of the Add button in the add existing item dialog box and the drop-down context menu should have "Add as link". Use that.
Jesse C. Slicer
I used that same approach while developing the Simple Service Locator. It works fine.
Steven
Having two projects in the same folder will cause pain down the road. You are much better off using Jesse's suggestion to link files.
Jonathan Allen
@Jonathan: I've never had problems with doing this - using Link Files is another option - but two projects in one directory only causes problems, in my experience, when you're trying to NOT share files. (Most of the Patterns and Practices projects are done with 2 projects in the same folder...)
Reed Copsey
@Reed I don't know. Why I have no reason to doubt your word, I can't help but worry about the obj and bin folders. Even if it works now, the next version of Visual Studio could break things.
Jonathan Allen
I'll check both methods... damn why can't I answer to answers as the correct one :p
TimothyP
@Jonathan: This is the approach most of the Microsoft open-source/shared-source projects take -so I doubt they'll break it. It's up to you what to do, of course, though ;)
Reed Copsey
A: 

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

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

<ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
sukru
+6  A: 

When you want to reuse code, you have basically three options:

  • Generally create all your basic class libraries as a Silverlight class library project, because it is the framework with the lowest set of features. Throw all references out except for mscorlib.dll, System.dll and System.Core.dll. You can then link such kind of Silverlight library in any full .NET project.

  • You can link individual code files from another project with the "Add as link" feature (Right click project -> Add Existing Item -> Change "Add" Button to "Add as link"). That way you can create a Silverlight project and link individual files from your full .NET project. However that can get tedious if you have a lot of files and you often add/remove files and folders in your source project.

  • To cure this problem, you may check out the Project Linker at http://msdn.microsoft.com/en-us/library/dd458870.aspx ... but I haven't tried it myself yet.

herzmeister der welten
Thank you for this, the last one really helps,I have however marked Reed's answer as the answer because he was first and answered my question specifically, your answer does help me a lot and I will look into the project linker, thank you.
TimothyP
A: 

As the comments in the accepted answer said, you use the arrow on the Add button in the file dialog to do "Add As Link."

To add one more small thing that may be of use: It's important to remember that Silverlight and .NET 4.0 aren't the same. If you have code that compiles in one but not the other, the dev tools by default define a SILVERLIGHT conditional compile symbol for Silverlight, so you can do #if SILVERLIGHT.

nlawalker
Hey, thnx. Yep I realize that, but the common code are just simple interface definitions :)
TimothyP