在xLua脚本内加载三方dll:xlua.load_assembly('aLuaTestDll') --不用加.dll后缀
使用C#内部类:CS.xxx.xxx.xxx CS前缀是固定的,后面命名空间和类名按c#调用约定拼写
cs
public xLuaForm()
{
InitializeComponent();
_luaEnv = new LuaEnv();
//_luaEnv.
}
public void Print(string msg)
{
MessageBox.Show(msg);
}
private LuaEnv _luaEnv;
public static void StaticMethod(string msg)
{
MessageBox.Show(msg);
}
private void button1_Click(object sender, EventArgs e)
{
var script = $@"
xlua.load_assembly('NLuaTestDll')
CS.System.Windows.Forms.MessageBox.Show('hello001')
local newForm = CS.System.Windows.Forms.Form()
newForm:ShowDialog();
newForm:Dispose();
person = CS.NLuaTestDll.Person()
person.Name = 'Winter'
person.Age = 10
CS.WindowsFormsApp1.xLuaForm.StaticMethod(person:ToString())
participant = {{""张三"", ""李四"", ""老王"", ""狗蛋"", ""铁剩""}}
CS.WindowsFormsApp1.xLuaForm.StaticMethod('abc')
--require 'CSharpCallLua'
--person = {{Name = 'Tom', Age = 10}}
name = 'jerry'
form.Text = 'lua test'
form:Print('hello ' .. #participant)
function add(a, b)
return a+b
end
";
_luaEnv.Global["form"] = this;
_luaEnv.DoString(script);
var p = _luaEnv.Global.Get<Person>("person");
MessageBox.Show(p.ToString());
}
cs
namespace NLuaTestDll
{
[LuaCallCSharp]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string ToString()
{
return $"{Name} -- {Age}";
}
}
}