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

推荐订阅源

L
LangChain Blog
博客园 - 司徒正美
美团技术团队
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
B
Blog
NISL@THU
NISL@THU
月光博客
月光博客
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
L
Lohrmann on Cybersecurity
The Cloudflare Blog
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
S
Secure Thoughts
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
H
Hacker News: Front Page
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
InfoQ
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
GRAHAM CLULEY
W
WeLiveSecurity
小众软件
小众软件
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - 右手边

这是一个广为流传的关于项目管理的通俗讲解 [史上最狂妄的演讲]你不服都不行<英汉版>~~ - 右手边 - 博客园 关于页面动态添加控件 利用反射动态创建对象 使用反射技术动态创建类对象(实例代码) 利用反射来动态创建实例和调用方法(上) 基于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); } } } 转载