Cross compile for .NET 4 and Silverlight 4 in VS2010 without duplicating files
We have a large number of projects within a solution mostly simple cla开发者_如何学运维ss 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.
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.
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.
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.)
Microsoft recently released the Portable Library Tools: http://msdn.microsoft.com/en-us/library/gg597391.aspx
I'll definitely have a look at these.
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>
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.
精彩评论