在我的“ 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环境中的重建版本也可能会有所帮助。
最佳答案
所以我也花了很多时间。真正让我疯狂的是试图让Enums工作。最终,我放弃了我的项目,选择了一个非常简化的控制台应用程序,该应用程序非常相似(具有讽刺意味的是也称为“ LuaTest”)。
编辑:我已经注意到,初始的“ luanet.load_assembly(“ LuaTest”)”显得多余。使用它,或者令人惊讶地没有它。
另一个修改:如我在下面删除时编辑不良的评论中所示:
print(luanet.LuaTest.Pointless)
一切都停止了工作(LuaTest.Pointless变为零)。但是添加luanet.load_assembly(“ LuaTest”)即可使其正常工作。可能是打印中或仅表示它们键入时有某种奇怪的隐式负载。非常奇怪(tm)。
无论如何,它似乎对我有用(请注意:经过大量实验)。我不知道您为什么会失败,我没有注意到任何真正的区别,但是这里是我的所有代码,以防其他人发现关键的区别:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaInterface;
namespace LuaTest
{
public class Program
{
static void Main(string[] args)
{
Lua lua = new Lua();
lua.DoFile("test.lua");
}
public int some_member = 3;
}
public class Pointless
{
public enum AnEnum
{
One,
Two,
Three
};
public static string aStaticInt = "This is static.";
public double i;
public string n = "Nice";
public AnEnum oneEnumVal = AnEnum.One;
private AnEnum twoEnumVal = AnEnum.Two;
private string very;
public Pointless(string HowPointLess)
{
i = 3.13;
very = HowPointLess;
}
public class MoreInnerClass
{
public string message = "More, please!";
}
public void Compare(AnEnum inputEnum)
{
if (inputEnum == AnEnum.Three)
Console.WriteLine("Match.");
else
Console.WriteLine("Fail match.");
}
}
}
和test.lua:
luanet.load_assembly("LuaTest")
--Pointless is a class in LuaTest assembly
local Pointless = luanet.import_type("LuaTest.Pointless")
print(Pointless)
--Gives 'ProxyType(LuaTest.Pointless): 46104728
print(Pointless.aStaticInt)
--'This is static.'
--Fails if not static, as we expect
--Instantiate a 'Pointless'.
local p = Pointless("Very")
print(p)
--Gives 'LuaTest.Pointless: 12289376'
--Now we can get at the items inside the Pointless
--class (well, this instance, anyway).
local e = p.AnEnum;
print(e)
--ProxyType(LuaTest.Pointless+AnEnum): 23452342
--I guess the + must designate that it is a type?
print(p.i)
--3.14
print(p.oneEnumVal)
--Gives 'One: 0'
print(p.twoEnumVal)
--Gives 'twoEnumVal'... private
--behaves very differently.
print(e.Two:ToString())
--Gives 'Two'
local more = p.MoreInnerClass()
print(more.message)
--'More, Please!'
--create an enum value here in the script,
--pass it back for a comparison to
--the enum.
local anotherEnumVal = p.AnEnum.Three
p:Compare(anotherEnumVal)
--outputs 'Match'