zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    jQuery
    jquery操作滚动条滚动到指定元素位置 scrollTop
    javascript的offset、client、scroll使用方法
    js javascript:void(0) 真正含义
    Js中数组Array的用法
    javascript中typeof、undefined 和 null
    函数的参数是函数,函数中Ajax返回的回调函数中的函数运行
    xampp中php手动升级
    jQuery Mobile里xxx怎么用呀? (事件篇)
    eval解析JSON中的注意点
  • 原文地址:https://www.cnblogs.com/baggiojing/p/1325297.html
Copyright © 2011-2022 走看看