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

推荐订阅源

I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 最新话题
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
The GitHub Blog
The GitHub Blog
V
V2EX
SecWiki News
SecWiki News
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
A
Arctic Wolf
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Register - Security
The Register - Security
Blog — PlanetScale
Blog — PlanetScale
The Last Watchdog
The Last Watchdog
T
Tor Project blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
P
Proofpoint News Feed
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
小众软件
小众软件
雷峰网
雷峰网
H
Heimdal Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
Webroot Blog
Webroot Blog
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
人人都是产品经理
人人都是产品经理
Hacker News: Ask HN
Hacker News: Ask HN
The Hacker News
The Hacker News
V
Vulnerabilities – Threatpost
Microsoft Security Blog
Microsoft Security Blog

博客园 - 右手边

这是一个广为流传的关于项目管理的通俗讲解 [史上最狂妄的演讲]你不服都不行<英汉版>~~ - 右手边 - 博客园 关于页面动态添加控件 利用反射动态创建对象 使用反射技术动态创建类对象(实例代码) 利用反射来动态创建实例和调用方法(上) 基于ASP.NET的Web动态控件创建 家中请客(最佳ERP教材)- - 2007年4月25日 经典的几句英文 网上英语免费杂志在钱订阅网址 将(Oracle)数据库表导出到Excel,并生成文件(C#实现) B/S结构与C/S结构 AJAX三步走 Ajax简单例子 Oracle 启动与关闭例程 AJAX的原理 VB资料库-无限下载 ASP.Net中程序构架与程序代码的分离 Oracle 系统结构
C#动态执行代码
右手边 · 2007-06-15 · via 博客园 - 右手边

Posted on 2007-06-15 13:46  右手边  阅读(1463)  评论()    收藏  举报

所谓动态代码执行,和脚本引擎有点类似。就是程序执行期从字符串或者文本文件中读取一段 C# 代码,在内存中动态编译成程序集,并创建相关类型实例执行相关方法。 具体的实现可参考下面的代码。如需要生成程序集文件,可参考《使用CodeDom生成程序集》。 using System; using System.Reflection; using System.Globalization; using Microsoft.CSharp; using System.CodeDom; using System.CodeDom.Compiler; namespace ConsoleApplication1 { public class Program { static void Main(string[] args) { // 定义需要动态执行的 C# 代码字符串,当然也可从文本文件中读取。 string code = @" using System; namespace MyNamespace { public class MyClass { private string name; public MyClass(string name) { this.name = name; } public void Test() { Console.WriteLine(""{0} - {1}"", name, DateTime.Now); } } } "; // 创建编译器对象 CSharpCodeProvider p = new CSharpCodeProvider(); ICodeCompiler cc = p.CreateCompiler(); // 设置编译参数 CompilerParameters options = new CompilerParameters(); options.ReferencedAssemblies.Add("System.dll"); options.GenerateInMemory = true; options.OutputAssembly = "MyTest"; // 开始编译 CodeSnippetCompileUnit cu = new CodeSnippetCompileUnit(code); CompilerResults cr = cc.CompileAssemblyFromDom(options, cu); // 执行动态程序集相关内容。DW&Q供^U3Oq理:N`K9)y;8中1XX^4`#教管o,N3专qQIC3-垠O育bH 3H+o管&Ich理&-Y Type t = cr.CompiledAssembly.GetType("MyNamespace.MyClass"); object o = cr.CompiledAssembly.CreateInstance("MyNamespace.MyClass", false, BindingFlags.Default, null, new object[] { "Tom" }, CultureInfo.CurrentCulture, null); t.InvokeMember("Test", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, o, null); } } } 转载