XNA C#脚本编写与CSharpCodeProvider

4

我正在使用XNA制作一个RPG风格的游戏,现在我正在实现一个脚本引擎。

我已经跟随了一些教程来尝试让它工作。目前我从XML文件中读取以下内容:

namespace MyGame
{
  public class EngagedCode : ScriptingInterface.IScriptType1
  {
    public string RunScript()
    {
      ChangeFrame( 2 );
    }
  }
}

在我成功将它引入项目后,我尝试使用以下代码进行编译:

Microsoft.CSharp.CSharpCodeProvider csProvider = new Microsoft.CSharp.CSharpCodeProvider();

CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = false; //DLL
options.GenerateInMemory = true;
options.IncludeDebugInformation = true;

options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);

CompilerResults result = csProvider.CompileAssemblyFromSource(options, code);

然而,此时我总是遇到以下错误:
'result.CompiledAssembly' threw an exception of type 'System.IO.FileNotFoundException'

似乎系统无法找到我编译的.dll文件,但我不知道原因。我不知道如何解决这个错误。有人有什么建议吗?

2个回答

4
即使你在内存中生成它,它仍然会将.dll文件写入磁盘,除非你存在编译错误,否则你会得到这个无用的System.IO.FileNotFoundException异常。因此,很可能是你有编译错误。
为了检查这些编译错误,您需要添加以下内容。
CompilerResults results = csProvider.CompileAssemblyFromSource(parameters, textBox1.Text);

if (results.Errors.Count > 0)
{
    foreach (CompilerError CompErr in results.Errors)
    {
        //Hooray a list of compile errors
    }
    else
    {
        //Successful Compile
    }
}

此外,如果您想跳过所有这些内容,请查看此类。它允许您只使用方法主体,但这对您可能不够。此外,您需要更改常量CodeStart字符串中的命名空间。

1

以下行不是必需的:

options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接