开发者

XNA C# Scripting with CSharpCodeProvider

I'm working on an RPG-style game in XNA and I'm working on implementing a scripting engine.

I've followed a few tutorials to try to get this working. Currently I read in the following from an XML file:

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

After I get that successfully into the project, I try to compile it with the following code:

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);

Comp开发者_如何学GoilerResults result = csProvider.CompileAssemblyFromSource(options, code);

However, at this point I always get the following error:

'result.CompiledAssembly' threw an exception of type 'System.IO.FileNotFoundException'

It seems as if the system is unable to find the .dll I've compiled, and I don't know why. I don't know how to get past this error. Does anybody have any suggestions?


Even if you generate it in memory it still writes a .dll to disk, unless you have compilation errors, and then you get this useless System.IO.FileNotFoundException. So most likely you have compile errors.

In order to pull those compile errors you need to add the below.

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
    }
}

Also if you want to skip all this. Take a look at this class. It allows you just use the method body, this may not be sufficient for you though. Also you will need to change the namespace in the const CodeStart string.


The following line is not required:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜