惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
博客园 - 叶小钗
V
V2EX
博客园 - Franky
博客园_首页
小众软件
小众软件
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
S
SegmentFault 最新的问题
B
Blog RSS Feed
博客园 - 【当耐特】
D
Docker
N
Netflix TechBlog - Medium

博客园 - pysharp

openstack horizon api step by step understanding horizon's template path what can PsTools\psexec do! - pysharp What's New in Python3.0 - pysharp [转]利用adsutil.vbs脚本创建自定义web站点 我的2008. sql基础篇,不断更新中...... TypeConvert Demo javascript 调试器v1.0.0.0 Dundas Chart Demo For New User - pysharp c#面向对象中的继承初步认识 .net 集合类初步认识 c# 目录操作类 c# 文件操作类 - pysharp - 博客园 关于urlrewrite的小DEMO - pysharp - 博客园 c# 读取Excel到datable asp.net 下载和在线预览Excel的方法 排序算法 c#实现 简单使用nHibernate,新手练习用。
c# 动态编译方法
pysharp · 2008-01-28 · via 博客园 - pysharp

今天写了一个Excel导入数据库验证的方法,验证规则是存到数据库里了,对于不同的Excel调用不同的验证方法。
代码:
        
using Microsoft.CSharp;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Globalization;

        CompilerResults result=null;
        /// <summary>
        /// 生成一个验证方法
        /// </summary>
        /// <returns></returns>
        public void DynamicInvokeValidate() {

            string code = this.compilerMethod();//拼接字符串方法。            

            //设置编译器对象
            CSharpCodeProvider csprovider = new CSharpCodeProvider();
            ICodeCompiler icompiler = csprovider.CreateCompiler();

            //设置编译环境
            CompilerParameters options = new CompilerParameters();  //参数对象
            options.ReferencedAssemblies.Add("System.dll");   //加载引用的程序集
            options.ReferencedAssemblies.Add("System.Data.dll");
            options.GenerateInMemory = true;     //是否输出到内存
            options.OutputAssembly="DynamicValidate"; //输出程序集的名称

            //开始编译
            CodeSnippetCompileUnit codesnipper = new CodeSnippetCompileUnit(code);
            this.result = icompiler.CompileAssemblyFromDom(options, codesnipper);
        }            public string DynamicMethod(DataRow row) {
            try
            {
                object[] parameters ={ row };
                Type t = this.result.CompiledAssembly.GetType("IngoingExcel.Validate");

                object obj = this.result.CompiledAssembly.CreateInstance("IngoingExcel.Validate", false, BindingFlags.Default, null, new object[] { }, CultureInfo.CurrentCulture, null);

                object str = t.InvokeMember("ValidateIngoingData", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, obj, parameters);

                return str.ToString();
            }
            catch
            {
                return "调用验证方法出错,请联系管理员.";                  
            }           
        }