这是我用来编译脚本的代码。
public static CompilerResults CompileScript(ScriptType type, string source)
{
if (ScriptUtil._compiler == null)
{
ScriptUtil._compiler = CodeDomProvider.CreateProvider("CSharp");
ScriptUtil._compilerParams = new CompilerParameters();
ScriptUtil._compilerParams.CompilerOptions = "/optimize";
ScriptUtil._compilerParams.GenerateInMemory = true;
ScriptUtil._compilerParams.GenerateExecutable = false;
ScriptUtil._compilerParams.IncludeDebugInformation = false;
foreach (AssemblyName reference in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
{
ScriptUtil._compilerParams.ReferencedAssemblies.Add(reference.Name + ".dll");
}
ScriptUtil._compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
}
ScriptUtil._compilerParams.OutputAssembly = @"..\DataSvr\Script" + Path.DirectorySeparatorChar + type.ToString() + Path.DirectorySeparatorChar + "Compiled" + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(source) + ".cmp";
CompilerResults results = ScriptUtil._compiler.CompileAssemblyFromFile(ScriptUtil._compilerParams, source);
if (File.Exists(ScriptUtil._compilerParams.OutputAssembly))
{
File.SetLastWriteTime(ScriptUtil._compilerParams.OutputAssembly, File.GetLastWriteTime(source));
}
ScriptUtil._compiler.Dispose();
return results;
}
我想让我的脚本简短,所以我从它们中删除了命名空间,并在它们继承的类所在的命名空间中添加了一个导入。
using MyGame.Game.Script;
脚本文件夹有GameScript
类,所有脚本都是从这个类继承的,但是,当我编译脚本并获得结果时 - 它表示它无法找到类型或命名空间{{1 }}。为什么会这样?它从未发生过。