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

推荐订阅源

爱范儿
爱范儿
量子位
人人都是产品经理
人人都是产品经理
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
Recent Announcements
Recent Announcements
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
H
Help Net Security
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
The Cloudflare Blog
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
Vercel News
Vercel News

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