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

推荐订阅源

The Cloudflare Blog
T
Tenable Blog
V
Vulnerabilities – Threatpost
T
Troy Hunt's Blog
SecWiki News
SecWiki News
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Secure Thoughts
Cyberwarzone
Cyberwarzone
A
Arctic Wolf
H
Heimdal Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
N
News and Events Feed by Topic
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
Forbes - Security
Forbes - Security
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
T
Tor Project blog
WordPress大学
WordPress大学
AWS News Blog
AWS News Blog
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
Cloudbric
Cloudbric
O
OpenAI News
U
Unit 42
Google DeepMind News
Google DeepMind News
Simon Willison's Weblog
Simon Willison's Weblog
Recorded Future
Recorded Future
N
News | PayPal Newsroom
S
Schneier on Security
F
Full Disclosure
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
P
Privacy International News Feed
L
LINUX DO - 最新话题
F
Fortinet All Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements

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