LuaInterface - 注册函数 对象参考

我正在尝试将 LuaInterface 集成到 C# 中,当我尝试将 C# 函数绑定到 Lua 时,我遇到了 System.NullReferenceException 错误。 我试图编译和运行的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuaInterface;

namespace Hobot
{
    class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            Lua lua = new Lua();
            lua.RegisterFunction("puts", null, typeof(Program).GetMethod("Test"));
            lua.DoFile("test.lua");
        }

        private void Test(String text)
        {
            Console.WriteLine(text);
        }
    }
}

当我运行这段代码时,我收到了一个 System.NullReferenceException 错误,错误信息为 Object reference not set to an instance of an object.,说它在 RegisterFunction 方法上崩溃了。

点赞
用户845947
用户845947

在RegisterFunction的目标参数中,您有“_null_”。

试试这个:

lua.RegisterFunction("puts", this, typeof(Program).GetMethod("Test"));
2012-11-28 08:54:36
用户1475906
用户1475906

我知道这个问题已经有些年头了,但是既然这里没有答案,我希望这个回答没问题:

它失败了,因为Test是私有的。将其改为

public void Test(String text)
{
    Console.WriteLine(text);
}

然后使用

lua.RegisterFunction("puts", this, GetType().GetMethod("Test"));

这样就应该解决了 (:

2014-05-27 19:43:18