我正在尝试为练习制作一个基本的游戏编辑器,我创建了自己的GameEngine图书馆。我正在尝试创建一个新程序并将GameEngine类添加到其中。这是我的代码。
GameEditor:
private void button1_Click(object sender, EventArgs e)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.dll", "System.Core.dll", "GameEngine.dll" }, "foo.exe", true);
parameters.GenerateExecutable = true;
string code =
@"
using System;
using System.Collections.Generic;
using System.Text;
using GameEngine;
class Program {
public static void Main(string[] args) {
Game.Start();
}
}";
CompilerResults results = csc.CompileAssemblyFromSource(parameters, code);
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
}
GameEngine.Game:
using System;
namespace GameEngine
{
public class Game
{
public static void Start()
{
Console.WriteLine("Game engine has been started!");
}
}
}
问题是每当我尝试使用Game.Start()
时foo.exe都会崩溃答案 0 :(得分:0)
此代码适用于我。我最初得到的唯一错误是
Unhandled Exception: System.BadImageFormatException:
Could not load file or assembly 'GameEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
File name: 'GameEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
at Program.Main(String[] args)
这是我的不好,因为我使用.Net v4.5编译了GameEngine
。但是,当我使用3.5进行编译并移动GameEngine.dll
旁边的foo.exe
时,它运行了
...\bin\Debug>foo.exe
Game engine has been started!
如果您收到错误
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly
'GameEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one
of its dependencies. The system cannot find the file specified.
File name: 'GameEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
at Program.Main(String[] args)
然后可执行文件找不到dll。自己查找dll,看看是否编译并存在于foo.exe
旁边