views:

2391

answers:

5

We wrote a small Windows class library that implements extension methods for some standard types (strings initially). I placed this in a library so that any of our projects would be able to make use of it by simply referencing it and adding using XXX.Extensions.

A problem came up when we wanted to use some of these methods in Silverlight. Although all the code was compatible, a Windows library can't be referenced in Silverlight so we created a Silverlight library that had links to the same class files and put compiler directives into the classes to allow different using declarations and namespaces. This worked fine until today when I added a new class to the Windows extensions library and realised that I would have to remember to link the class into the Silverlight library too.

This isn't ideal and I wondered if anyone might have ideas for a better way of sharing extension methods and other helper code between Windows and Silverlight projects.

+2  A: 

there is a similar problem with XNA projects. Since you can target several different platforms, you're required to have different projects. The reason for this is because the base class libraries that the project references are platform specific, so you can't have just one project.

If you're curious, you can get a bit of insight from this blog:

To recompile the source for another platform, you need another project. The reason for this is because the projects need to reference different assemblies for both the XNA Framework and the underlying .NET Framework (Xbox 360 and Zune use the .NET Compact Framework), and C# projects don't provide support for referencing different assemblies for different platforms.

Joel Martinez
The quote is not absolutely right)
Rinat Abdullin
+3  A: 

Silverlight runtime is different from the normal .NET runtime. So you need to do tricks at the project level to share code between multiple platforms.

Here's how I've done this for Autofac IoC container.

With this approach you do not have to create different projects for each platform being targeted.

PS: there is also a Project Linker tool from the Composite WPF that allows to link Silverlight and WPF projects (creates multiple projects). But it does look messy.

Rinat Abdullin
+7  A: 

You cannot set a reference from a Silverlight assembly to a regular .NET assembly but you can do so the other way round.

So create a shared Silverlight assembly and add your code to that assembly. Now you can set a reference fro both your regular .NET and you other Silverlight assembly to the shared Silverlight assembly.

The restriction is that you can only put code in there that would work on both the .NET and Silverlight CLR but that is no different from sharing code.

Maurice
+1: I'm doing exactly that with a library that must run on both smart devices and desktops. It's called retargetability (see http://msdn.microsoft.com/en-us/magazine/cc163387.aspx).
Andreas Huber
This does not appear to be the case. I created a quick Silverlight library to test this but when trying to reference it from a Windows library project I get "Silverlight projects can only be referenced by other Silverlight projects."
Steve Crane
@Steve: Have you tried to just reference the dll instead of the whole project?
Andreas Huber
@Andreas, yes I now see that I can make a binary reference to the DLL but not a reference to the project. So this seems like a good solution.
Steve Crane
@Steve: I have also just set a reference to a Silverlight class library project from regular .NET class library project and that worked just fine. I wonder why the same didn't work for you.
Maurice
@Maurice: That's cool, I can reference the Silverlight project from a Windows library project here at work. My guess is that I still have the Silverlight 2 RC0 version on my home machine, which I don't use much for dev. Suppose I should update it.
Steve Crane
+1  A: 

Try this http://buildassilverlight.codeplex.com/

Rajat Mehta
A: 

I had some dependency issues when referencing to a Silveright class library in .Net.

An alternative way when using Visual Studio 2010 and WCF RIA 1.0:

  • Create a normal .Net library assembly.
  • Create a Silverlight class library. In the configuration of the assembly specifiy the first .NET library as the "WCF RIA Service link"
  • Put your code in the .NET library as "ClassName.shared.cs" files.
  • WCF RIA will handle copying the file to the Silverlight assembly.
Johan