Lua is a good extensibility language, and the LUA interpreter is designed as a library that is easily embedded into the host program. Luainterface is used to implement mixed programming for LUA and the CLR.
(i) C # calls Lua
Test environment: Build a C # console application in VS2015 and add a reference to LuaInterface.dll
luainterface:http://luaforge.net/projects/luainterface/(Download luainterface-1.5.3, there are more resources)
The Luainterface.lua class is the primary interface for the CLR to access the LUA interpreter, and a Luainterface.lua class object represents a LUA interpreter (or LUA execution Environment), and the LUA interpreter can be multiple at the same time, and they are completely independent of each other.
Simply describe the following steps:
Add a reference to the LuaInterface.dll at the root directory:
Not verbose, directly on the code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingLuainterface;7 8 namespaceCsharptolua9 {Ten class Program One { A Static voidMain (string[] args) - { - //create a new LUA interpreter, each of which is independent of each LUA instance, and a global domain theLua LUA =NewLua (); - - //--------------------------------------------------- - //the index operation of Lua [] can create, access, modify the global domain +lua[" Age"] = -; -lua["name"] ="Mr.huang"; + A stringLuacode ="print (\ "This is Lua code\")"; atLua. Dostring (Luacode);//executing LUA scripting code -Lua. Dofile ("F:\\csharptolua\\csharptolua\\scriptsfromfile.lua");//executes the Lua script file, where I use absolute positioning directly. - - DoubleAge = (Double) lua[" Age"]; - -Console.WriteLine ("Age = {0}", age); inConsole.WriteLine ("width = {0}", lua["width"]); - Console.readkey (); to } + } - the}
You can hit it again, improve the intimacy with the code ~.~ and then click Start
Hey, there will be an error, but it does not matter,
Solution:
Run again, you can see the results, surprised not surprise, not surprisingly
A simple introduction to C # How to invoke Lua code, and of course, a lot of rich API small partners can self-check yo.
(ii)Lua calls C #
Directly on the code ~.~
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingLuainterface;7 8 namespaceCsharptolua9 {Ten class Program One { A Static voidMain (string[] args) - { - //create a new LUA interpreter, each of which is independent of each LUA instance, and a global domain theLua LUA =NewLua (); - - - //calling C # functions---------------------------------------------------Lua +TestClass obj =NewTestClass (); - + //registers a CLR object method to LUA for Lua to invoke typeof (TestClass). GetMethod ("Testprint") ALua. Registerfunction ("Testprint", obj, obj. GetType (). GetMethod ("Testprint")); at - //registering CLR static methods to LUA for LUA invocation -Lua. Registerfunction ("Teststaticprint",NULL,typeof(TestClass). GetMethod ("Teststaticprint")); - -Lua. Dostring ("Testprint (Ten)"); -Lua. Dostring ("Teststaticprint ()"); in - Console.readkey (); to } + } - the classTestClass * { $ Private intValue =0;Panax Notoginseng - Public voidTestprint (intnum) the { +Value =num; AConsole.WriteLine ("CSharp"+value); the } + - Public Static voidTeststaticprint () $ { $Console.WriteLine ("Teststaticprint"); - } - } the}
Click to run ....... .....
Perfect~.~ of course, here is just a simple introduction to how C # and Lua call each other, small partners can self-Google a large number of rich APIs to facilitate the development yo
Important things to say three times: Luainterface is the primary interface, Luainterface is the primary interface, Luainterface is the primary interface
C # and Lua call each other