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