views:

32

answers:

3

Is there a way to do this?

I have a library with 'helper' code wich I want tp use from a Silverlight project. I don't know why, but Visual Studio won't let me add the reference even though there's nothing prohibited in my library (like DataSets)

I don't want to write all that code again, so I was thinking maybe creating a Silverlight class library and adding my classes as links to that project.

I know that works for other stuff, havn't tried it in Silverlight yet, is there a problem with that approach? is there a better way to achieve what I want?

+1  A: 

No you can't add a .net assembly, you need to recompile the library for silverlight.

You can create a new empty Silverlight class library, in the same directory as the .net classlibrary. Then choose "Show All Files" in the project menu. Then you'll see all .cs files in that folder half transparent in the solution explorer. Select them and choose "Include in Project" in the context-menu.

It might even be possible to just use one project with several builds(like Debug and Release some of which target SL), but I'm not sure if that's possible. This would have the advantage that you don't need to keep the project files in sync manually.

CodeInChaos
I know I can't, that's what I'm saying... I just want to use the classes from my lib in a Silverlight lib as links and add that lib as a reference
sebastian
@seb - the distinction between *rewriting* and *rebuilding* is a rather large one.
Hans Passant
@hans the answer was not complete when I entered that comment... now it says what I needed to know, thanks
sebastian
+1  A: 

There is no problem with that approach. MEF, for example, has done that. You'll find two sets of projects in there - a desktop and a silverlight project, each which refer to the same .cs files.

One nice thing to help, as well - if there is any non-silverlight functionality, or SL specific functionality you require in your classes, you can use partial classes to handle those cases. Just put include the second .cs file in the appropriate projects, and use partial classes, and you can separate out normal/sivlerlight functionality.

Reed Copsey
+1  A: 

Hi Sebastian,

There shouldn't be a problem with that approach. As a matter of fact, Prism makes heavy use of linked files (in combination with pre-processor directives) to support both WPF and Silverlight by the library.

Additionally, you can use the Project Linker tool to help you with this.

I hope this helps.

Damian Schenkelman