C # mutual calls with lua,

Source: Internet
Author: User

C # mutual calls with lua,

Lua is a good scalability language. The Lua interpreter is designed as a library that is easily embedded into the host Program. LuaInterface is used to implement mixed programming of Lua and CLR.

(1) C # Call Lua

  Test environment: Create a C # console application in VS2015 and add a reference to LuaInterface. dll.

LuaInterface: http://luaforge.net/projects/luainterface/ (download luainterface-1.5.3, where there are more resources)

LuaInterface. the Lua class is the main interface for CLR to access the Lua interpreter, a LuaInterface. a Lua class object represents a Lua interpreter (or Lua execution environment). The Lua interpreter can exist multiple at the same time, and they are completely independent of each other.

The following steps are briefly described:

  

Add a reference to LuaInterface. dll in the root directory:

  

  

  

Run the Code directly:

  

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 using LuaInterface; 7 8 namespace CSharpToLua 9 {10 class Program11 {12 static void Main (string [] args) 13 {14 // create a Lua interpreter, each Lua instance is independent of each other. A global full-region 15 Lua lua = new Lua (); 16 17 // --------------------------------------------------- 18 // Lua index operation [] allows you to create, access, and modify the global domain 19 lua ["age"] = 20; 20 lua ["name"] = "Mr. huang "; 21 22 string luaCode =" print (\ "This is Lua code \") "; 23 lua. doString (luaCode); // execute the lua script code 24 lua. doFile ("F :\\ CSharpToLua \ ScriptsFromFile. lua "); // execute the lua script file. Here I use the absolute location 25 26 double age = (double) lua [" age "]; 27 28 Console. writeLine ("age = {0}", age); 29 Console. writeLine ("width = {0}", lua ["width"]); 30 Console. readKey (); 31} 32} 33 34}

You can repeat it to increase the intimacy with the code ~.~ And then click to start

Warning. An error will be reported, but it doesn't matter,

  

  Solution:

  

Run it again and you will see the results.

  

A Brief Introduction to C # How To Call The Lua code, of course, there are a lot of API friends can refer to it on their own.

 

(2)LuaCall C #

Directly upload the code ~.~

 

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 using LuaInterface; 7 8 namespace CSharpToLua 9 {10 class Program11 {12 static void Main (string [] args) 13 {14 // create a Lua interpreter, each Lua instance is independent of each other. A global full-region 15 Lua lua = new Lua (); 16 17 18 // ------------------------------------------- lua calls c # function 19 TestClass obj = new TestClass (); 20 21 // register the CLR object method to Lua for Lua to call typeof (TestClass ). getMethod ("TestPrint") 22 lua. registerFunction ("TestPrint", obj, obj. getType (). getMethod ("TestPrint"); 23 24 // register the CLR static method to Lua for Lua to call 25 lua. registerFunction ("TestStaticPrint", null, typeof (TestClass ). getMethod ("TestStaticPrint"); 26 27 lua. doString ("TestPrint (10)"); 28 lua. doString ("TestStaticPrint ()"); 29 30 Console. readKey (); 31} 32} 33 34 class TestClass35 {36 private int value = 0; 37 38 public void TestPrint (int num) 39 {40 value = num; 41 Console. writeLine ("CSharp" + value); 42} 43 44 public static void TestStaticPrint () 45 {46 Console. writeLine ("TestStaticPrint"); 47} 48} 49}

 

Click Run .................

Perfect ~.~ Of course, here is a brief introduction of how C # And Lua call each other. Small partners can easily develop a large number of Google APIs.

Important: LuaInterface is the main interface, LuaInterface is the main interface, and LuaInterface is the main interface

 

Related Article

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.