LUA and. NET CLR call each other

Source: Internet
Author: User

Construction Environment:
    • Download the Luainterface-1.5.3.zip file, use the DLL as LuaInterface.dll, Lua51.dll, Luanet.dll in the built directory
    • LuaInterface.dll is a C # DLL that needs to be referenced in the project
    • Lua51.dll, Luanet.dll for C + + DLLs, need to be copied to the output directory of the project (DEBUG)
    • C # calls Lua to use: LuaInterface.dll and Lua51.dll
    • The classes that LUA calls C # need to use: LuaInterface.dll and Luanet.dll,luanet.dll copied to the project output directory, and the Lua script does not need to use Package.cpath and require.

Many of the posts did not clearly lead to an error:

' luanet '  from ' . \luanet.dll '
That's what I'm giving you, brother Yun.
1 namespaceTestlua2 {3      Public class Program4     {5 6          Public stringName ="Zhaozongyun";7         Static voidMain (string[] args)8         {9             //Create a LUA interpreter (also known as a LUA virtual machine)TenLua LUA1 =NewLua (); One  AConsole.WriteLine ("Execute LUA Strings--->> C #"); -  -             //execute LUA strings in C # theLua1. Dostring ("num = 111"); -Lua1. Dostring ("str = ' Zhaozongyun '"); -Object[] values = lua1. Dostring ("return num, str"); -             foreach(Object objinchvalues) +             { - Console.WriteLine (obj); +             } A  atConsole.WriteLine ("--->> C # Register the normal method into Lua and execute the LUA string"); -  -             //C # Registers the normal method into Lua, and executes the LUA string -Program P =NewProgram (); -Lua LUA2 =NewLua (); -Lua2. Registerfunction ("LuaMethod1", p, P.gettype (). GetMethod ("Clrmethod")); inLua2. Dostring ("LuaMethod1 ()"); -  toConsole.WriteLine ("--->> C # Register static methods into Lua and execute LUA strings"); +  -             //C # Registers a static method into Lua and executes the LUA string theLua2. Registerfunction ("LuaMethod2",NULL,typeof(program). GetMethod ("Staticmethod")); *Lua2. Dostring ("LuaMethod2 ()"); $ Panax NotoginsengConsole.WriteLine ("Execute LUA Scripts--->> C #"); -  the             //execute Lua script files in C # +Lua LUA3 =NewLua (); ALua3. Dofile ("Mylua1.lua"); the  +Console.WriteLine ("--->>c# executes lua scripts, loads C # classes in Lua scripts, and accesses C #"); -  $             //C # executes LUA files, LUA files load C # classes, and calls C # $Lua LUA4 =NewLua (); -Lua4. Dofile ("Mylua2.lua"); -Lua4. Dofile ("Mylua3.lua"); the  - Console.readkey ();Wuyi         } the  -          Public voidClrmethod () Wu         { -Console.WriteLine ("Clrmethod"); About         } $  -          Public Static voidStaticmethod () -         { -Console.WriteLine ("Staticmethod"); A         } +     } the}

LUA1 Script

1 222 2 string " Hello World " 3 Print (string)

LUA2 Script

1 -- loading CLR types, instantiating CLR objects 2 luanet.load_assembly ("System")3 Int32 = Luanet.import_type ("system.int32") 4 Print (Int32)

LUA3 Script

1 --require "luanet"2 --types of loading CLR3Luanet.load_assembly ("Testlua")4program = Luanet.import_type ("Testlua.program")5 --instantiating a CLR object6program =Program ()7 Print(Program.name)8Program:clrmethod ()

LUA and. NET CLR 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.