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; }
        }
    }


  • 相关阅读:
    Centos下Zookeeper的安装部署
    Zookeeper入门
    Redis高可用-主从,哨兵,集群
    Redis入门
    centos7 安装redis6.0.3
    二叉树的遍历及常用算法
    分享一个seata demo,讲两个个问题
    互联网公司,我们需要什么样的中层技术管理以及996和程序员有多大关系?
    Spring Boot微服务如何集成seata解决分布式事务问题?
    软件服务架构的一些感悟
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/1768516.html
Copyright © 2011-2022 走看看