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

推荐订阅源

C
CERT Recently Published Vulnerability Notes
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
L
LangChain Blog
博客园_首页
有赞技术团队
有赞技术团队
The Register - Security
The Register - Security
GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
量子位
雷峰网
雷峰网
Security Latest
Security Latest
博客园 - 【当耐特】
V2EX - 技术
V2EX - 技术
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
IT之家
IT之家
爱范儿
爱范儿
S
Schneier on Security
N
News | PayPal Newsroom
H
Help Net Security
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
U
Unit 42
博客园 - 司徒正美
Forbes - Security
Forbes - Security
P
Proofpoint News Feed
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
The Cloudflare Blog
AWS News Blog
AWS News Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
Google DeepMind News
Google DeepMind News
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
G
GRAHAM CLULEY

博客园 - 右手边

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