LuaInterface的提示

來源:互聯網
上載者:User

using LuaInterface;   //記得引用 LuaInterface.DLL到工程中

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        class testclass   //這個類用來存放要開放給LUA的函數
        {
            public void ShowMessage(string s)
            {
                MessageBox.Show(s);
            }
        }
        private void button1_Click(object sender, EventArgs e) 
        {
            Lua lua = new Lua();

            testclass a1 = new testclass();
            lua.RegisterFunction("ShowMessage", a1, a1.GetType().GetMethod("ShowMessage")); //註冊函數給LUA

 

            try

            {

                 object[] obj = new object[2]; //先申請對象

                 //LUA中調用 C#的函數                

                 lua.DoString(“ value = 0 /nfunction add(a,b)/n return a+b,a*b /n end /n value = add(1,2) /nShowMessage(value)”);

                 // 執行指令碼,顯示計算結果3

 

                 //C#中調用 LUA的函數
                 LuaFunction add=(LuaFunction)lua.GetFunction("add");

                 if(add != null)  //由於從指令碼中擷取函數,有可能擷取不到,所以這裡要做容錯

                      obj = add.Call(new object[] {2,3});

 

                 //這個時候 obj[0] 就等於5,obj[1] 就等於6  函數返回多個值

                 Double val=lua.GetNumber("value"); //擷取指令碼中的變數值

            }

            finally

            {

                 ShowMessage("執行完成");

            }
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.