C# Luainterface,如何从c代码传递整个类?

C# Luainterface,如何从c代码传递整个类?,c#,compiler-construction,lua,luainterface,C#,Compiler Construction,Lua,Luainterface,我想把整个类从c#代码传递到lua,这样我就可以在lua中创建一个新对象并使用它的方法、字段等等。完成后,我想知道是否可以在lua中使用对象,这些对象是在c#代码中创建的,然后以某种方式传递给lua 这是我的密码: atm,不可能在我的luainterface中创建Person1类的对象,然后使用它的函数 当我在LuaScripter中输入say()时(没有在那里创建任何对象),我将得到Person2.say()的输出 在这行中,您可以使用person1:say()或person2:say()

我想把整个类从c#代码传递到lua,这样我就可以在lua中创建一个新对象并使用它的方法、字段等等。完成后,我想知道是否可以在lua中使用对象,这些对象是在c#代码中创建的,然后以某种方式传递给lua

这是我的密码: atm,不可能在我的luainterface中创建Person1类的对象,然后使用它的函数 当我在LuaScripter中输入say()时(没有在那里创建任何对象),我将得到Person2.say()的输出

在这行中,您可以使用person1:say()或person2:say()

如果你的类有很多函数和属性,那么传递整个类是很耗时的

using System;
using LuaInterface;
using System.Reflection;

namespace ConsoleApplication
{


public class Person1
{
    public void say()
    {
        Console.WriteLine("person1 says: hehe");
    }
}


public class Person2
{
    public void say()
    {
        Console.WriteLine("person2 says: hihi");
    }
}

class Class1
{
    static void Main(string[] args)
    {
        Lua lua_compiler = new Lua();

        Person1 person1 = new Person1();
        Person2 person2 = new Person2();

        lua_compiler.RegisterFunction("say", person1, person1.GetType().GetMethod("say"));
        lua_compiler.RegisterFunction("say", person2, person2.GetType().GetMethod("say"));


        while (true)
        {
            string line = Console.ReadLine();
            try { lua_compiler.DoString(line); }
            catch { }
        }
    }
}
}
    Person1 person1 = new Person1();
    Person2 person2 = new Person2();

    lua_compiler["person1"] = person1;
    lua_compiler["person2"] = person2;


    while (true)
    {
        string line = Console.ReadLine();
        try { lua_compiler.DoString(line); }
        catch { }
    }