东成西就

路漫漫兮 其修远兮 吾将上下而求索!

在.NET中动态编译与执行脚本文件

  前段时间群里有位朋友问到如何在.NET环境下实现脚本引擎的,今天简单的来说说。要在.NET下实现脚本语言其实可以有多种方式,而我采用了最为简单有效的办法Stick out tongue - 动态编译并执行。通过动态编译技术可以得到如下几方面的好处:

  1、利用CLR本身的完整性来处理脚本,使得你的脚本可以支持几乎.NET本身支持的一切特性,记得这里是几乎全部而不是所有的;

  2、不用自己费时来写脚本解释器,能够及大的提高你所在项目的开发速度;

  3、采用编译的方式执行脚本程序从性能上来说肯定比解释的方式要快,至于.NET程序本身就通过JIT来执行的我们暂且不论;

  4、有现成的IDE可供使用如VS.NET、C# Builder或其它的工具,使得开发人员的学习曲线变得很短。

  OK,言归正转,不然的话大家该扔砖头啦,我挡,我挡,我挡挡挡Surprise要在.NET环境中使用动态编译技术需要在你的项目中引入System.CodeDom.Compiler;命名空间,关于它的描述或作用的话大家可以查看一下msdngoogle一把结果肯定比我说的详细,在此就不再啰嗦啦。下面以我写的一个小演示项目为例详细向大家说明如何在项目中使用动态编译技术。

  一、引入相应的命名空间

1
2
using System.Reflection;
using System.CodeDom.Compiler;

  如果你需要对VB.NET、JS进行支持的话,还需要在项目引用中加入Microsoft.VisualBasic及Microsoft.JSscript的引用。

  二、编写简单的脚本引擎组件

  现在我们可以着手来完成属于咱们“自己”的脚本引擎了,在我的例子中类名为CSScriptEngine。对于这个简单的脚本引擎我们只简单的的申明了如下公共接口以便用户使用。

  1、public CSScriptEngine() 脚本引擎的默认构造函数,在不知道要编译的脚本文件及相关参数时使用;

  2、public CSScriptEngine(string filename, string[] args) 脚本引擎构造函数,用于直接初始化需要编译并执行的脚本文件;

  3、public void Run() 编译并执行脚本文件;

  4、public void Run(string filename, string[] args) Run()函数的重载版本用于编译并执行指定的脚本文件,之所以这样是为了减少在实例化脚本引擎方面所带来的性能损耗,如果在系统中有多处要使用到脚本引擎的话可以使用池来管理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        /// <summary>
/// 脚本引擎默认构造函数
/// </summary>
public CSScriptEngine()
{
}


/// <summary>
/// 脚本引擎构造函数
/// </summary>
/// <param name="filename">脚本文件</param>
/// <param name="args">编译参数</param>
public CSScriptEngine(string filename, string[] args)
{
this.sourcefile = filename;
this.args = new string[args.Length];
for (int i = 0; i != args.Length; i++)
this.argsIdea = argsIdea;
}


/// <summary>
/// 执行脚本文件
/// </summary>
public void Run()
{
if (args.Length < 1)
args = new string[] { "/e" };

Complie();
if (HasArgs(args, "/e"))
InvokeMainPoint();
}


/// <summary>
/// 执行脚本文件
/// </summary>
/// <param name="filename">脚本文件名</param>
/// <param name="args">编译参数</param>
public void Run(string filename, string[] args)
{
this.sourcefile = filename;
this.args = new string[args.Length];
for (int i = 0; i != args.Length; i++)
this.argsIdea = argsIdea;
Run();
}
  正如你所看到的一样,我们对外公开的接口简单实现的代码也十分短小。可能你已经注意到了只有当编译参数包含"/e"才通过InvokeMainPoint()函数来执行脚本代码;其次,因为通过"/e"参数将脚本直接编译成了可执行文件,所以在第二次执行时就不用再编译了;最后,这个脚本引擎还支持将脚本文件编译为程序集(.dll文件)。

  那么CSScriptEngine背后都做了些什么工作呢?首先,定义了相关的辅助成员变量:

1
2
3
4
5
        private CodeDomProvider codeProvider = null;
private CompilerParameters compilerParameters = null;
private CompilerResults compilerResults = null;
private string sourcefile = string.Empty;
private string[] args = null;

  第1行定义了Dom提供者,它是我们的脚本引擎的主角用于实现代码的编译工作;第2行是编译参数对象;第3行所定义的complierResults用于存储编译结果,如果在编译的过程中发生错误都可以通过它来获取,同时其中也保存着编译后的程序集,在后面我们会用到;最后两行定义了待编译的源文件及外部参数。

  下面让我们看看用于实现脚本文件编译的详细过程,由于在代码中已经有详细的注释便不再解释每个函数的作用,大家看了代码自然就明白啦。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        #region Assistant functions
        /// <summary>
/// 根据源代码文件名创建编译器
/// </summary>
/// <param name="filename">源代码文件名</param>
private void CreateCodeCompiler(string filename)
{
string fileExtension = Path.GetExtension(filename);
switch (fileExtension.ToLower())
{
case ".cs" :
case ".csharp" :
codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
break;

case ".vb" :
case ".vb.net" :
case ".visualbasic" :
codeProvider = new Microsoft.VisualBasic.VBCodeProvider();
break;

case ".js" :
case ".jscript" :
codeProvider = new Microsoft.JScript.JScriptCodeProvider();
break;


default :
throw new Exception("Sjteksoft.Framework.ScriptEngine 暂时未提供对 \"" + fileExtension + "\" 类文件的动态编译能力。");
}
}


/// <summary>
/// 动态编译
/// </summary>
private void Complie()
{
if (sourcefile == null || sourcefile == string.Empty)
throw new Exception("没有提供待编译的源文件,不能未完成编译任务。");

            // 分析编译参数
string outputfile = string.Empty;
if (HasArgs(args, "/e"))
outputfile = Path.ChangeExtension(sourcefile, ".exe");
else
outputfile = Path.ChangeExtension(sourcefile, ".dll");
ParseCompilerArgs(outputfile, args);

CreateCodeCompiler(sourcefile);
compilerResults = codeProvider.CompileAssemblyFromFile(compilerParameters, sourcefile);

            // 检查编译错误
if (compilerResults.Errors.Count > 0)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i != compilerResults.Errors.Count; i++)
{
CompilerError error = compilerResults.ErrorsIdea;
sb.Append(error.IsWarning ? "Warning :" : "Error :" + " (" + error.Line.ToString() + ", " + error.Column.ToString() + ")" + error.ErrorText + " filename : " + error.FileName);
}
sb.Append("");
throw new Exception(sb.ToString());
}
}


/// <summary>
/// 检查是否含有某参数
/// </summary>
/// <param name="args">参数列表</param>
/// <param name="arg">指定参数</param>
/// <returns>如果参数列表中含有指定参数则返回true,否则返回false</returns>
private bool HasArgs(string[] args, string arg)
{
for (int i = 0; i != args.Length; i++)
{
if (argsIdea.StartsWith(arg))
return true;
}
return false;
}


/// <summary>
/// 执行编译后的程序集的主程序
/// </summary>
private void InvokeMainPoint()
{
if (compilerResults == null || compilerResults.CompiledAssembly == null)
throw new Exception("脚本源文件尚未编译不能执行。");
            // 查询脚本文件中的入口点
MethodInfo method = null;
foreach (Module module in compilerResults.CompiledAssembly.GetModules())
{
foreach (Type type in module.GetTypes())
{
foreach (MemberInfo member in type.GetMembers(BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase))
{
if (member.Name.ToLower() == "main")
{
method = type.GetMethod(member.Name, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase);
break;
}
}
if (method != null)
break;
}
if (method != null)
break;
}


// 执行脚本程序
if (method != null)
{
try
{
method.Invoke(null, null);
}
catch (Exception ex)
{
throw ex;
}
}
else
throw new Exception("没有找到到主程序入口点。");

}


/// <summary>
/// 分析编译参数
/// </summary>
/// <param name="args">参数列表</param>
private void ParseCompilerArgs(string outputfile, string[] args)
{
if (compilerParameters == null)
compilerParameters = new CompilerParameters();


compilerParameters.CompilerOptions = string.Empty;
for (int i = 0; i < args.Length; i++)
{
if(argsIdea.StartsWith("/e"))
{
compilerParameters.GenerateExecutable = true;
compilerParameters.GenerateInMemory = false;
compilerParameters.OutputAssembly = Path.GetExtension(outputfile) == ".exe" ? outputfile : Path.ChangeExtension(outputfile, ".exe");
}


if (argsIdea.StartsWith("/d"))
{
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = false;
string filename = Path.GetExtension(outputfile) == ".dll" ? outputfile : Path.ChangeExtension(outputfile, ".dll");
compilerParameters.OutputAssembly = filename;
}
}
            compilerParameters.CompilerOptions += " /optimize";
compilerParameters.ReferencedAssemblies.Add("System.dll");
compilerParameters.ReferencedAssemblies.Add("System.Data.dll");
compilerParameters.ReferencedAssemblies.Add("System.Xml.dll");
}
        #endregion

  代码很简单Stick out tongue

  三、主调程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Sjteksoft.Framework.Core.ScriptEngine;


namespace Sjteksoft.Framework.Core.CSScript
{
class Program
{
static void Main(string[] args)
{
if(args.Length < 1)
{
ShowUsage();
return;
}

if (args.Length >= 1)
{
string[] complieArgs = new string[args.Length - 1];
for (int i = 1; i != args.Length; i++)
{
complieArgs[i - 1] = argsIdea;
}


StringBuilder sb = new StringBuilder();
sb.Append("Sample C# Script Engine. Version " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\n");
sb.Append("Copyright(C) by Sjteksoft 2006, All rights reserved.");
Console.WriteLine(sb.ToString());

CSScriptEngine scriptEngine = new CSScriptEngine(args[0], complieArgs);
scriptEngine.Run();
}
}

/// <summary>
/// 显示使用说明
/// </summary>
private static void ShowUsage()
{
StringBuilder sb = new StringBuilder();
sb.Append("Sample C# Script Engine. Version " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\n");
sb.Append("Copyright(C) by Sjteksoft 2006, All rights reserved.\n");
sb.Append("\n");
sb.Append("Usage : " + new FileInfo(Application.ExecutablePath).Name + " <file> [switch]\n");
sb.Append("\n");
sb.Append("<file> - 将执行或编译的脚本文件\n");
sb.Append("\n");
sb.Append("[switch]\n");
sb.Append("/e - 编译为可执行文件并执行\n");
sb.Append("/d - 编译为程序集\n");
sb.Append("\n");
Console.WriteLine(sb.ToString());
}
}
}

  因为是简单的演示程序,所以写的是控制台程序,这样也方便大家使用。以下是这个脚本引擎执行的结果:

  OK,就这么多啦,关于如何在脚本程序与主程序之间进行通信及异步调用执行脚本文件将在今后的时间里给大家奉上。在附件中是完整的项目代码。
  原文地址

posted on 2006-12-19 13:05  东成西就  阅读(2320)  评论(1编辑  收藏  举报

导航