zoukankan      html  css  js  c++  java
  • C#动态编译代码

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.CSharp;
    using System.IO;
    using System.CodeDom.Compiler;

    namespace CompileFiles
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //*************************
                IDictionary<stringstring> dic = new Dictionary<stringstring>();
                dic.Add(
    "CompilerVersion""v3.5");
                CSharpCodeProvider objCSharpCodePrivoder 
    = new CSharpCodeProvider(dic);

                CompilerParameters paras 
    = new CompilerParameters();
                paras.GenerateExecutable 
    = false;   //编译成exe还是dll
                
    //paras.ReferencedAssemblies.Add("System.dll");
                paras.GenerateInMemory = true;   //是否写入内存,不写入内存就写入磁盘
                paras.OutputAssembly = "C:\\UserInfo.dll";  //输出路径
                paras.ReferencedAssemblies.Add("System.dll");
                paras.ReferencedAssemblies.Add(
    @"C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll");
                StreamReader sr 
    = new StreamReader(@"C:\UserInfo.cs");
                String txtStr 
    = sr.ReadToEnd();

                CompilerResults result 
    = objCSharpCodePrivoder.CompileAssemblyFromSource(paras, txtStr);
                
    if (result.Errors.HasErrors)
                {
                    
    string ErrorMessage = "";
                    
    foreach (CompilerError err in result.Errors)
                    {
                        ErrorMessage 
    += err.ErrorText;
                    }
                    Console.WriteLine(ErrorMessage);
                }
                
    //*************************
                Console.ReadKey();
            }
        }
    }
    // -----------------------------------------
    using System;


    namespace Model
    {
        
    public class UserInfo
        {
            
    public virtual int ID { getset; }
            
    public virtual string UserID { getset; }
            
    public virtual string UserName { getset; }
        }
    }


  • 相关阅读:
    Python 操控Mysql
    mysql5.7 root密码重置
    pandas合并两个excel到一个excel
    键盘控制
    激活浏览器窗口
    python的xlwings库读写excel操作总结
    python 读取Excel使用xlwing库
    CMDB(资产管理系统) day1
    Vue之简易的留言板功能
    vue之神奇的动态按钮
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/1768516.html
Copyright © 2011-2022 走看看