【问题标题】:XNA C# Scripting with CSharpCodeProvider使用 CSharpCodeProvider 编写 XNA C# 脚本
【发布时间】:2011-10-02 01:13:29
【问题描述】:

我正在 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,不知道为什么。我不知道如何克服这个错误。有人有什么建议吗?

【问题讨论】:

    标签: c# scripting xna


    【解决方案1】:

    即使你在内存中生成它,它仍然会将 .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
        }
    }
    

    如果你想跳过这一切。看看this class。它允许你只使用方法体,但这对你来说可能还不够。您还需要更改 const CodeStart 字符串中的命名空间。

    【讨论】:

      【解决方案2】:

      以下行不是必需的:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-18
        • 2021-05-21
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 2016-09-04
        • 2011-09-04
        • 1970-01-01
        相关资源
        最近更新 更多