美文网首页
动态编译基础

动态编译基础

作者: forsek | 来源:发表于2017-10-19 13:43 被阅读0次
一. 将函数或方法放在文本文件或dll文件或字符串中,可以修改添加后,直接使用主程序调用改变后的功能。(把方法和主程序分离,方便修改与添加)
using System;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

class Program
{
    public static void Main()
    {
        // The C# code to execute,分离出的方法,字符串形式,方法名PrintConsole,参数message,功能:打印message信息
        string code = "using System; " +
               "using System.IO; " +
               "public class MyClass{ " +
               "  public static void PrintConsole(string message){ " +
               "    Console.WriteLine(message); " +
               "  } " +
               "} ";

        // Compiler and CompilerParameters,实例化编译器与编译参数,用来编译分离的内容
        CSharpCodeProvider codeProvider = new CSharpCodeProvider();
        CompilerParameters compParameters = new CompilerParameters();

        // Compile the code,编译后,返回编译结果
        CompilerResults res = codeProvider.CompileAssemblyFromSource(compParameters, code);

        // Create a new instance of the class 'MyClass'    // 有命名空间的,需要命名空间.类名
        object myClass = res.CompiledAssembly.CreateInstance("MyClass");

        // Call the method 'PrintConsole' with the parameter 'Hello World',调用PrintConsole方法,使用"Hello World"参数。
        // "Hello World" will be written in console
        myClass.GetType().GetMethod("PrintConsole").Invoke(myClass, new object[] { "Hello World" });

        Console.Read();

[原文链接](http://www.jb51.net/article/117946.htm

相关文章

  • 动态编译基础

    一. 将函数或方法放在文本文件或dll文件或字符串中,可以修改添加后,直接使用主程序调用改变后的功能。(把方法和主...

  • 【Java基础】动态编译

    源代码:https://gitee.com/AgentXiao/reflection动态编译要点:1、使用场景(在...

  • 改善Java程序建议17

    建议 17: 慎用动态编译。 关于动态编译的代码: 只要静态编译能做的事情,动态编译就能实现。 动态编译时,需要注...

  • 动态代理反射

    动态代理 分类AspectJ编译-静态编译-AspectJ编译编译器JDK动态代理-接口cglib动态代理-子类(...

  • iOS面试题

    iOS面试准备 基础 1. 为什么说Objective-C是一门动态的语言? 编译期:即编译器对语言的编译阶段,编...

  • c++编译跨平台动态库

    window编译动态库 linux编译动态库 makefile文件

  • iOS 面试题 -- 2017基础篇

    基础 1、为什么说Objective-C是一门动态的语言? 静态、动态是相对的,这里动态语言指的是不需要在编译时确...

  • go语言简介

    1 基础知识 go语言有以下特点: 编译型语言,编译速度快 静态类型语言,拥有动态类型语言特点 类c语法,简单易学...

  • day09集合 字符编码 数据类型详解

    Ⅰ 基础 python是一门解释型的强类型动态语言 编译型:在执行程序前有一个单独的编译过程,将程序翻译成...

  • Java的动态机制---动态编译

    Java的动态机制---动态编译1 作用 客户写代码,动态编译。 服务器动态加载某些类文件 2 两种做法 通过Ru...

网友评论

      本文标题:动态编译基础

      本文链接:https://www.haomeiwen.com/subject/oluuuxtx.html