views:

528

answers:

2

I'm trying to understand a little about the relationship of server-side code to client side code in Silverlight.

I would anticipate that you can't simply 'eval' a string and have new code, but could you load an assembly on the server side and include it with the Silverlight code that is sent down to the client?

I'm a complete 'noob' when it comes to Silverlight so I may be completely misunderstanding how it works so any clarification would be appreciated.

[Edit]

Just so it makes a little more sense what I'm trying to do, I'd like to write some simple code (as in logic only), compile it on the server and then send it back down to the client so it can be used on the client side. I'm not sure if that would be a DLL or even possible...

A: 

Are you interested in running dynamic languages on the Silverlight platform? If so, you're in luck:

http://silverlight.net/learn/dynamiclanguages.aspx/

Dynamic code generation in C# on the other hand would be considered a type of reflection, and that's not currently possible in Silverlight (see System.Reflection.Emit in the full .NET Framework for comparison).

Edit 1: This doesn't sound like the question though you still might want to consider it. Here's a good article on how to dynamically generate Xaml on the server, package it up and return it to the client:

http://msdn.microsoft.com/en-us/magazine/2009.01.cuttingedge.aspx

The part of the equation that's still missing is how to generate a SL dll on the server (likely using the Reflection.Emit namespace). Those dlls can be added to the Xap as mentioned in the article and sent back to the client.

Here's another way to dynamically load dlls into SL from the server: http://www.shinedraw.com/data-handling/flash-vs-silverlight-loading-external-assemblylibrary-dynamically/

Now I'm not certain if you can dynamically generate SL dlls but I can check.

James Cadd
+2  A: 

This guy has a working downloadable sample doing exactly what you want:

http://www.nokola.com/trycsharp/HowToBuild.aspx

It basically uses the Microsoft.CSharp.CSharpCodeProvider to compile the code, but uses the Sivlerlight Dlls so it works properly.

You can even compile a whole silverlight page (xaml & cs), but you have to do it using msbuild as it needs to do a pre-compile pass to generate the .g.cs file that wires up the Xaml objects to the C# code.

Both methods are server-side only.

mattmanser