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

推荐订阅源

博客园 - 叶小钗
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
量子位
月光博客
月光博客
人人都是产品经理
人人都是产品经理
U
Unit 42
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
美团技术团队
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
D
Docker
B
Blog
大猫的无限游戏
大猫的无限游戏
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog

博客园 - 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 "调用验证方法出错,请联系管理员.";                  
            }           
        }