0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その646

0
Posted at

概要

cscの作法、調べてみた。
Neo.IronLua、見つけたので、やってみた。

参考にしたページ

Neo.IronLua(通常は NeoLua と呼ばれる)は、.NET アプリケーション内で Lua言語(主にLua 5.2)を動かすためのスクリプトエンジンライブラリです。
C# / .NET との高い連携性:C# から Lua の変数・関数を簡単に呼び出したり、逆に Lua スクリプト内から .NET のクラス、メソッド、インターフェース、イベントを直接操作したりできます。

環境

windows10

サンプルコード


using System;
using Neo.IronLua;

class Program {
	static void Main() {
		// 1. Luaエンジンの初期化
		using (Lua lua = new Lua())
		{
			// 2. 独立した実行環境(グローバル環境)の作成
			LuaGlobal global = lua.CreateEnvironment();
			// 3. C#の変数をLua側に公開する
			global["message"] = "こんにちは、Neo.IronLuaの世界へ!";
			global["addFunc"] = new Func<int, int, int>((a, b) => a + b);
			// 4. スクリプトの実行
			string script = @"
				print(message)
				local result = addFunc(10, 20)
				print('計算結果:', result)
				return 'Luaからの返り値'
			";
			// チャンク(スクリプト断片)としてコンパイルして実行
			LuaResult chunkResult = global.DoChunk(script, "test.lua");
			// 5. Luaからの戻り値を受け取る
			Console.WriteLine(chunkResult[0]);
		}
	}
}




実行手順

>csc neo.cs -r:neo.lua.dll
Microsoft (R) Visual C# Compiler version 4.8.9232.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240



>neo
こんにちは、Neo.IronLuaの世界へ!
計算結果: 30
Luaからの返り値


>

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?