在运行时编译 C#

发布于 2024-11-07 06:46:25 字数 1342 浏览 1 评论 0原文

我想在我的游戏中实现 C# 作为脚本语言。

我的问题是,如果我想使用游戏核心(exe)中定义的类,我的脚本将无法编译。

该脚本如下所示:

using System;
using ConsoleApplication1;

class Script
{
private static void Call()
{
    Console.WriteLine("called");
}

public static void Init()
{
    Console.WriteLine("Script");
    Call();
    GameObject myO; // THIS IS WHAT I WANT TO GET WORKED,
 //IF THIS IS COMMENTED OUT, IT COMPILES FINE, GAMEOBJECT
 // IS DEFINED IN THE "ConsoleApplication1" NAMESPACE.
}
}

该脚本的编译方式与 MDX 示例中类似:

    static void Main(string[] args)
    {
        CodeDomProvider provider = new CSharpCodeProvider();
        CompilerParameters cp = new CompilerParameters();
        //cp.CompilerOptions = "/target:library";
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
        cp.IncludeDebugInformation = false;
        cp.ReferencedAssemblies.Add("ConsoleApplication2.exe");
        cp.ReferencedAssemblies.Add("System.dll");
        CompilerResults cr = provider.CompileAssemblyFromFile(cp, "script.cs");
        if (!cr.Errors.HasErrors)
        {
            Console.WriteLine("Success");
            cr.CompiledAssembly.GetType("Script").GetMethod("Init").Invoke(null, null);
        }
        Console.ReadLine();
    }

有没有办法通过脚本调用函数或创建“ConsoleApplication1”命名空间中定义的对象?

I want to implement C# as the scripting language in my game.

My problem is, that my script will not compile if I want to use classes defined in the game core (exe).

The script looks like this:

using System;
using ConsoleApplication1;

class Script
{
private static void Call()
{
    Console.WriteLine("called");
}

public static void Init()
{
    Console.WriteLine("Script");
    Call();
    GameObject myO; // THIS IS WHAT I WANT TO GET WORKED,
 //IF THIS IS COMMENTED OUT, IT COMPILES FINE, GAMEOBJECT
 // IS DEFINED IN THE "ConsoleApplication1" NAMESPACE.
}
}

The script is compiled like in the MDX sample:

    static void Main(string[] args)
    {
        CodeDomProvider provider = new CSharpCodeProvider();
        CompilerParameters cp = new CompilerParameters();
        //cp.CompilerOptions = "/target:library";
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
        cp.IncludeDebugInformation = false;
        cp.ReferencedAssemblies.Add("ConsoleApplication2.exe");
        cp.ReferencedAssemblies.Add("System.dll");
        CompilerResults cr = provider.CompileAssemblyFromFile(cp, "script.cs");
        if (!cr.Errors.HasErrors)
        {
            Console.WriteLine("Success");
            cr.CompiledAssembly.GetType("Script").GetMethod("Init").Invoke(null, null);
        }
        Console.ReadLine();
    }

Is there any way to call functions or create objects defined in the "ConsoleApplication1" namespace via the script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2024-11-14 06:46:25

这是一个日常编程问题。它不起作用,而你认为​​它应该起作用。所以把它分解一下。不要解决一个大问题,而是解决一个较小的问题。只需尝试在程序外部编译脚本即可。一旦您开始工作,您就可以尝试从程序内部将其编译为脚本,因为您已经解决了引用和编译器问题的基本问题。

像这样的东西:

csc /reference:ConsoleApplication1.exe script.cs

从表面上看,这可能就像将引用从 ConsoleApplication2.exe 更改为 ConsoleApplication1.exe 一样简单。

This is a daily programming problem. It's not working and you think it should be working. So break it down. Instead of working on a big problem, work on a smaller problem. Just tackle trying to compile the script outside of your program. Once you get that working, then you can try to compile it as a script from inside your program, knowing that you've got the basic problem of references and compiler issues sorted out.

Something like:

csc /reference:ConsoleApplication1.exe script.cs

From the looks of it, it might be as simple as changing the reference from ConsoleApplication2.exe to ConsoleApplication1.exe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文