C # invoke scripting language lua--simple Demo

Source: Internet
Author: User
Tags lua

C # invoke scripting language lua--simple Demo

Configuration:
1. Download the LUA support class library under C #. : http://files.luaforge.net/releases/luainterface/luainterface/2.0.3
Refer (Lua51.dll\luainterface.dll) to your project.
2. Modify app. Config to add the following:

<?xml version= "1.0" encoding= "Utf-8"?><configuration> <startup  Uselegacyv2runtimeactivationpolicy= "true" >    <supportedruntime version= "v4.0" SKU = ". netframework,version=v4.0 "/>    <supportedruntime version=" v2.0.50727 "/>  </startup></ Configuration>



Otherwise, the following prompt appears when you run the code:
Mixed-mode assemblies are generated for the "v2.0.50727" version of the runtime and cannot be loaded in the 4.0 runtime without additional information being configured.

To invoke a step:
1. Declaring a LUA virtual machine
Lua M_lua = new Lua ();
2. Register the C # object method with Lua so that LUA can invoke the method.
Class MyClass {
public string MyStr (string s)
{
return S + "world!";
}
}
MyClass my = new MyClass ();
M_lua. Registerfunction ("MyStr", My, My. GetType (). GetMethod ("MyStr"));

3. Loading LUA code
M_lua. Dofile ("Lua_test.lua");

4. Calling the Lua method
object[] Objs = M_lua. GetFunction ("MyNum"). Call (100);


Demo Code:
Using luainterface;using system;using system.collections.generic;using system.linq;using System.Text;using System.threading.tasks;namespace lua_test{class MyClass {public string mystr (string s) {RET        Urn S + "world!";        }} class Program {public static Lua M_lua = null;             Create LUA virtual machine static public void init () {MyClass my = new MyClass (); Create a custom class instance//register the custom function in the LUA virtual machine (global) and call the function M_lua in the Lua file. Registerfunction ("MyStr", My, My. GetType ().            GetMethod ("MyStr")); M_lua.           Dofile ("Lua_test.lua");            Load Lua file (absolute path)} static void Main (string[] args) {M_lua = new Lua ();            Init (); After loading the scrambled file, use getfunction to get the function, then call execute (pass parameter) object[] Objs = M_lua. GetFunction ("MyNum").            Call (100);            The return value of the Call function is an object array, foreach (Object obj in Objs) {Console.WriteLine (obj); } ConSole.        ReadLine (); }    }}

Demo Download:

http://download.csdn.net/detail/e421083458/8432515


PS: The simplest way to learn is to get a demo that can run.


C # invoke scripting language lua--simple Demo

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.