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

推荐订阅源

D
DataBreaches.Net
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Secure Thoughts
G
GRAHAM CLULEY
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
O
OpenAI News
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 最新话题
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
H
Hacker News: Front Page
W
WeLiveSecurity
P
Privacy International News Feed
Forbes - Security
Forbes - Security
月光博客
月光博客
PCI Perspectives
PCI Perspectives
T
Tailwind CSS Blog
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
Engineering at Meta
Engineering at Meta
F
Full Disclosure
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
Schneier on Security
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Visual Studio Blog
The Hacker News
The Hacker News
博客园 - 叶小钗
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss

博客园 - 右手边

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