• 7 Easy Steps to Learn C#: Silverlight C# Compiler and Loading DLLs from a server at runtime in Silverlight


    In This Tutorial:

    ·         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

    View sample

    What Is It Good For?

    ·         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!

    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;

                }

     

    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!

    Published Sunday, July 27, 2008 8:35 AM by nikola

  • 相关阅读:
    windows环境配置多个tomcat
    navicat 12 破解
    Eclipse创建Maven报异常:Could not get the value for parameter encoding for plugin......
    tomcat设置日志打印到文件中
    修改mysql数据库的休眠时间
    spring的bean标签的常用属性
    redis 安装与下载(windows版本)
    mysql设置远程可访问
    WPF非轮询方式更新数据库变化SqlDependency(数据库修改前台自动更新)
    WPF实战案例-在线程内同步集合数据到UI线程
  • 原文地址:https://www.cnblogs.com/baggiojing/p/1325297.html
Copyright © 2020-2023  润新知