C # invoke scripting language lua--simple Demo
Configuration:
1. Download the LUA support class library under C #. : http://files.luaforge.net/releases/luainterface/luainterface/2.0.3
Refer (Lua51.dll\luainterface.dll) to your project.
2. Modify app. Config to add the following:
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <startup Uselegacyv2runtimeactivationpolicy= "true" > <supportedruntime version= "v4.0" SKU = ". netframework,version=v4.0 "/> <supportedruntime version=" v2.0.50727 "/> </startup></ Configuration>
Otherwise, the following prompt appears when you run the code:
Mixed-mode assemblies are generated for the "v2.0.50727" version of the runtime and cannot be loaded in the 4.0 runtime without additional information being configured.
To invoke a step:
1. Declaring a LUA virtual machine
Lua M_lua = new Lua ();
2. Register the C # object method with Lua so that LUA can invoke the method.
Class MyClass {
public string MyStr (string s)
{
return S + "world!";
}
}
MyClass my = new MyClass ();
M_lua. Registerfunction ("MyStr", My, My. GetType (). GetMethod ("MyStr"));
3. Loading LUA code
M_lua. Dofile ("Lua_test.lua");
4. Calling the Lua method
object[] Objs = M_lua. GetFunction ("MyNum"). Call (100);
Demo Code:
Using luainterface;using system;using system.collections.generic;using system.linq;using System.Text;using System.threading.tasks;namespace lua_test{class MyClass {public string mystr (string s) {RET Urn S + "world!"; }} class Program {public static Lua M_lua = null; Create LUA virtual machine static public void init () {MyClass my = new MyClass (); Create a custom class instance//register the custom function in the LUA virtual machine (global) and call the function M_lua in the Lua file. Registerfunction ("MyStr", My, My. GetType (). GetMethod ("MyStr")); M_lua. Dofile ("Lua_test.lua"); Load Lua file (absolute path)} static void Main (string[] args) {M_lua = new Lua (); Init (); After loading the scrambled file, use getfunction to get the function, then call execute (pass parameter) object[] Objs = M_lua. GetFunction ("MyNum"). Call (100); The return value of the Call function is an object array, foreach (Object obj in Objs) {Console.WriteLine (obj); } ConSole. ReadLine (); } }}
Demo Download:
http://download.csdn.net/detail/e421083458/8432515
PS: The simplest way to learn is to get a demo that can run.
C # invoke scripting language lua--simple Demo