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


  • 相关阅读:
    Django 用ModelForm批量保存form表单(非常实用的方法) mfor_verity项目
    jquery ajax异步提交表单数据的方法
    python字符串转换成变量的几种方法
    django 线上线下使用不同的数据库 上线:mysql 线下sqlite3 以及debug模式的开和关
    django admin 或xdmin list_display search_fields list_filter 如果显示搜索外键或多对多字段
    nonce和timestamp在Http安全协议中的作用
    Web API接口 安全验证
    .Net环境下的缓存技术介绍
    .Net缓存管理框架CacheManager
    在asp.net web api中利用过滤器设置输出缓存
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/1768516.html
Copyright © 2011-2022 走看看