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


  • 相关阅读:
    Sketch or Figma谁才是UI设计软件的未来?
    2020年网站首屏设计:最佳实践和例子
    为设计工具付费到底值不值得?
    社交网站原型模板分享
    摹客PS插件,支持自定切图尺寸!
    UI设计适合女生学吗?会不会很难?
    mysql数据操作之单表查询
    表操作
    库操作
    数据库
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/1768516.html
Copyright © 2020-2023  润新知