· C# compiler demo for the web - type code in a web page, compile and execute it on the client
· How to enable dynamic code compilation for a Silverlight application
· How to execute third party DLL from within a Silverlight application
Download source code
· The compiled code is fast. It runs full speed on the client.
· It enables very flexible programs easily. Here are some example uses:
o Procedural texture generation (Perlin noise, 3-d textures, clouds, landscapes, and other). This is my next demo, btw.
o Fractals. The ability to compile code on-the-fly is very nice for experiments with fractals
o Advanced option that enables huge flexibility of LOB apps. Good for filtering database queries and such
o Simple plugins to extend a program
· As a side effect of using this technique, you’ll learn how to delay load Silverlight code – usable for making huge apps that load themselves part by part only when needed
· It’s good for educational purposes. Inspire more people to try and start coding!
The C# compiler sample does not compile on the client machine. Instead, the C# source is sent to a web service and compiled there.
Then on the client side Silverlight loads the DLL that is returned from the web service and executes it.
Perhaps the most interesting part of this sample is loading and assembly at runtime from within the Silverlight client application:
I’ve seen several heavy and not-so-obvious implementations on the web. It’s just a few lines of code, once you know how.
AssemblyPart part = new AssemblyPart();
MemoryStream stream = new MemoryStream(e.Result.AssemblyRawData);
_compiledAssembly = part.Load(stream);
Another interesting part is the web service compilation code:
With small modification, you can change it to target VB or WPF. Look at CompileHelper.cs in the source code.
parameters = new System.CodeDom.Compiler.CompilerParameters();
parameters.GenerateInMemory = true;
parameters.OutputAssembly = outputAssemblyFileName;
parameters.TreatWarningsAsErrors = false;
parameters.WarningLevel = 4;
parameters.TempFiles.KeepFiles = false;
if (TargetFramework == TargetFramework.Silverlight)
// Silverlight only: ignore the rsp file, because we want to replace the
// default desktop .net framework mscorlib and other "default" dlls
// with the Silverlight ones
parameters.CompilerOptions = " /nostdlib ";
provider = new Microsoft.CSharp.CSharpCodeProvider();
results = provider.CompileAssemblyFromSource(parameters, text);
errors = new List<ErrorInfo>(results.Errors.Count);
foreach (CompilerError error in results.Errors)
ErrorInfo info = new ErrorInfo();
info.Column = error.Column;
info.ErrorNumber = error.ErrorNumber;
info.ErrorText = error.ErrorText;
info.FileName = error.FileName;
info.IsWarning = error.IsWarning;
info.Line = error.Line;
if (results.Errors.Count == 0)
catch (Exception e)
errors = new List<ErrorInfo>();
Here are the 7 easy steps to learn C#, in my opinion: http://www.nokola.com/trycsharp/LearnCSharp.aspx
That’s all. Hope you’ll like this sample!