Silverlight Tutorial: Making the Silverlight C# Compiler Sample

Download source code

View sample

What Is It Good For?

On-the-fly web enabled C# compiler is very nice:

·         The compiled code is fast. It with 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!

Design 

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.

Implementation

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.

            Microsoft.CSharp.CSharpCodeProvider provider;

            CompilerParameters parameters;

            CompilerResults results;

 

            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 ";

            }

            parameters.ReferencedAssemblies.AddRange(referencedAssemblies);

 

            try

            {

                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;

                    errors.Add(info);

                }

 

                if (results.Errors.Count == 0)

                {

                    return results.CompiledAssembly;

                }

 

                return null;

 

            }

            catch (Exception e)

            {

                Debug.WriteLine(e.Message);

                errors = new List<ErrorInfo>();

                return null;

            }

 

That’s all. Hope you’ll like this sample!

Back to sample