首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LuaInterface - RegisterFunction对象引用

LuaInterface - RegisterFunction对象引用
EN

Stack Overflow用户
提问于 2012-11-26 09:56:28
回答 2查看 3.7K关注 0票数 2

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

代码语言:javascript
复制
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);
        }
    }
}

当我运行这段代码时,我得到一个带有Additional information: Object reference not set to an instance of an object.System.NullReferenceException,说它在RegisterFunction方法上崩溃了。

EN

回答 2

Stack Overflow用户

发布于 2012-11-28 16:54:37

RegisterFunction的目标参数中有null。

试试这个:

代码语言:javascript
复制
lua.RegisterFunction("puts", this, typeof(Program).GetMethod("Test"));
票数 3
EN

Stack Overflow用户

发布于 2014-05-28 03:43:19

我知道这个问题有点老了,但是看到这里没有答案,我希望这是可以的:

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

代码语言:javascript
复制
public void Test(String text)
{
    Console.WriteLine(text);
}

并使用

代码语言:javascript
复制
lua.RegisterFunction("puts", this, GetType().GetMethod("Test"));

这应该可以解决这个问题(:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13557672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档