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

  • 相关阅读:
    打开App显示文件已损坏,打不开,您应该将它移到废纸篓,怎么办?
    Mac下制作openwrt U盘启动盘
    iOS 修改打包后的.ipa应用名字
    使用Aria2+Aria2Ng+OneIndex+OneDrive建立不限流量/离线BT下载/在线观看网盘/在线存储分享平台
    使用微软易升安装纯净版win10
    Mac 配置adb环境变量(为了开Appium)亲测
    CocoaPods 安装及使用(亲测有效)
    1.6 MySQL 基础教程
    Rhythmk 一步一步学 JAVA (19) JAVA IO 文件常用操作
    Rhythmk 一步一步学 JAVA (18) Axis2 创建 WebService
  • 原文地址:https://www.cnblogs.com/baggiojing/p/1325297.html
Copyright © 2011-2022 走看看