【发布时间】:2012-11-07 03:44:06
【问题描述】:
在我的“LuaTest”命名空间中,我有一个名为“Planet”的类。 C# 代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaInterface;
namespace LuaTest
{
public class Planet
{
public Planet(string name)
{
this.Name = name;
}
public Planet() : this("NoName") { }
public string Name
{
get;
private set;
}
public void printName()
{
Console.WriteLine("This planet's name is {0}", Name);
}
}
}
然后我构建了 LuaTest.dll 并将此文件复制到保存我的 Lua 脚本的同一文件夹中。在我写的 Lua 脚本中:
--define Path for required dlls
package.cpath = package.cpath .. ";" .. "/?.dll"
package.path = package.path .. ";" .. "/?.dll/"
require 'luanet'
luanet.load_assembly("LuaTest")
local Planet = luanet.import_type("LuaTest.Planet")
local planet = Planet("Earth")
planet.printName()
但是,这段代码不起作用。 Lua解释器抛出这个错误:
lua: dllTest.lua:7: attempt to call local 'Planet' (a nil value)
我怀疑我的 LuaTest 程序集根本没有加载。谁能指出我做错了什么?非常感谢,因为我已经被这个问题困扰了好几天了。
另外,添加我的 LuaInterface.dll 是 .NET4.0 环境中的重建版本可能会有所帮助。
【问题讨论】:
-
我在使用 LuaInterface 时遇到了一些实际问题。您可能想查看 C# Compiling on the fly。
-
@Ruirize 感谢您的评论。那些“真正的问题”让你最终放弃了这种方法吗?我将看看 C# Compiling on the fly。希望不复杂。
-
基本上,如果没有长时间的变通方法,我无法将我的 C# 类公开给 Lua,这会使生成的 Lua 代码非常难看。 (我已经失去了这个项目,这是不久前的事了)C# 编译非常简单,你可以通过使用访问修饰符来控制暴露的内容。
-
问题是在这件事上我们必须坚持Lua。故事是,我们有一个用 Lua 编写的相当复杂的 ToolBox,现在我们想迁移到 .NET 框架。但是工具箱中的所有方法仍然必须像以前的好方法一样可以在 Lua 脚本中访问。这就是 LuaInterface 发挥作用的地方。但是,它不适用于此特定应用程序。
-
祝你狩猎愉快,好运。
标签: c# .net lua luainterface