具有NLua的C#类的实例

我正在尝试在 Lua 文件中使用 NLua 创建 C# 类的实例

这是我的代码。

class Program
{
    public static void Main (string[] args)
    {

        Lua lua = new Lua();
        lua.LoadCLRPackage();

        lua.DoString(@"
                        import ('LuaTest.exe', 'LuaTest')
                        test = Test()
                    ");
    }
}

public class Test
{
    public Test()
    {
        Console.WriteLine("IT WORKED");
    }
}

但这似乎不起作用,我寻找了许多不同的方法并尝试了许多种方式。我大多数尝试的错误是这样的:

An unhandled exception of type 'NLua.Exceptions.LuaScriptException' occurred in NLua.dll Additional information: [string "chunk"]:3: attempt to call global 'Test' (a nil value)

这有点奇怪,因为这是直接从他们的示例代码中拿出来的? https://github.com/NLua/NLua

感谢大家的帮助。

有点发牢骚:

如果我做错了什么,请告诉我。 另外,我正在使用纯 C# 构建,不确定这是否在这里有什么区别,我没有看到任何警告?但整个东西似乎文档寥寥无几。

如果有人对更好的纯 C# Lua 库有任何建议,我将乐意倾听。

点赞
用户4527057
用户4527057

你的 class 和 method 缺少 test annotations

class Program
{
    public static void Main (string[] args)
    {

        Lua lua = new Lua();
        lua.LoadCLRPackage();

        lua.DoString(@"
                        import ('LuaTest.exe', 'LuaTest')
                        test = Test()
                    ");
    }
}

[TestFixture]
public class Test
{
    [Test]
    public Test()
    {
        Console.WriteLine("IT WORKED");
    }
}
2015-04-26 04:27:37
用户198353
用户198353

很抱歉。README文件是错误的。

只需删除“.exe”,就应该可以正常工作

class Program
{
    public static void Main (string[] args)
    {

        Lua lua = new Lua();
        lua.LoadCLRPackage();

        lua.DoString(@"
                        import ('LuaTest', 'LuaTest')
                        test = Test()
                    ");
    }
}

public class Test
{
    public Test()
    {
        Console.WriteLine("IT WORKED");
    }
}

https://github.com/NLua/NLua

我已经从github.com/nlua/nlua修复了README。

2015-04-27 15:59:46