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.Errors
;
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 (args
.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(args
.StartsWith("/e"))
{
compilerParameters.GenerateExecutable = true;
compilerParameters.GenerateInMemory = false;
compilerParameters.OutputAssembly = Path.GetExtension(outputfile) == ".exe" ? outputfile : Path.ChangeExtension(outputfile, ".exe");
}
if (args
.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