C # and Lua call each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.