References:
1. Chapter 6 of C # advanced programming: Reflection
2. rcfalcon CSDN Blog: Package Lua for C # http://www.bkjia.com/kf/201204/127960.html
This article only discusses what is feature customization and how to combine C # With LUA scripts to form our applications.
Using System;
Using System. Text;
Using System. Reflection;
Namespace ConsoleApplication3
{
Public class mainClass
{
Static void Main (string [] args)
{
Console. WriteLine (GETMYNAME );
Console. Read ();
}
[CGYClass ("CaiGuangyue")]
Public static string GETMYNAME
{
Get
{
Return "CGY 'name ";
}
}
Public string CGYComment;
}
[AttributeUsage (AttributeTargets. Property, AllowMultiple = false, Inherited = true)]
Class CGYClassAttribute: Attribute
{
Private string CGYName;
Public cgyclassattriname (string cgyName)
{
This. CGYName = cgyName;
}
Public string GetCGYName ()
{
Return this. CGYName;
}
}
}
This is a test example I wrote.
. Net FrameWork allows users to define their own features. If we apply these custom features to our projects, we can load plug-ins or modules when developing scalable applications, it is undoubtedly very Happy.
Why do we need to mention the custom features in reflection?
We will use feature customization in the following example. Using feature customization, we can easily encapsulate our own LUA application to facilitate our use in LUA scripts.
Let's take a simple example:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. IO;
Using System. Net;
Using LuaInterface;
Namespace LUATest
{
Public partial class frmMain: Form
{
Private Lua MyLua = new Lua ();
Private string FileName;
Public frmMain ()
{
InitializeComponent ();
}
Private void btnFileName_Click (object sender, EventArgs e)
{
OpenFileDialog ofg = new OpenFileDialog ();
Ofg. Filter = "LUA script file (*. lua) | *. lua | all files (*. *) | *.*";
If (ofg. ShowDialog () = DialogResult. OK)
{
TxtFileName. Text = ofg. FileName;
FileName = ofg. FileName;
StreamReader sr = new StreamReader (ofg. FileName, Encoding. Default );
TxtFileContent. Text = sr. ReadToEnd ();
Sr. Close ();
}
}
/// <Summary>
/// Echo Method Used in LUA to add the execution result msg of LUA to ListBox txtResult
/// </Summary>
/// <Param name = "msg"> execution result </param>
Public void Echo (string msg)
{
TxtResult. Items. Add (msg );
}
Private void btnRun_Click (object sender, EventArgs e)
{
MyLua. RegisterFunction ("Echo", this, this. GetType (). GetMethod ("Echo "));
MyLua. DoFile (FileName );
}
}
}
After this simple example is completed, we use the feature customization in reflection to encapsulate our own applications.
Custom features:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Reflection;
Namespace LUATest
{
/// <Summary>
/// Feature class
/// </Summary>
[AttributeUsage (AttributeTargets. Method, AllowMultiple = false, Inherited = false)]
Class Lua_ClsAttribute: Attribute
{
Private string _ luaFuncName;
/// <Summary>
/// Feature class Constructor
/// </Summary>
Public lua_clsattriame (string luaFuncName)
{
This. _ luaFuncName = luaFuncName;
}
Public string GetLuaFuncName ()
{
Return this. _ luaFuncName;
}
}
}
Our own LUA application Library:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Reflection;
Using System. Windows;
Using LuaInterface;
Namespace LUATest
{
/// <Summary>
/// This type of CSDN Blog collected from rcfalcon: Package Lua for C #
// URL: http://www.bkjia.com/kf/201204/127960.html
/// </Summary>
Class LUAFrame
{
/// <Summary>
/// Lua Engine
/// </Summary>
Private Lua pLuaVM = new Lua (); // lua Virtual Machine
/// <Summary>
/// Register the lua Function
/// </Summary>
/// <Param name = "pLuaAPIClass"> lua function class </param>
Public void BindLuaApiClass (Object pLuaAPIClass)
{
Foreach (MethodInfo mInfo in pLuaAPIClass. GetType (). GetMethods ())
{
Foreach (Attribute attr in Attribute. GetCustomAttributes (mInfo ))
{
String LuaFunctionName = (attr as lua_clsattriame). GetLuaFuncName ();
PLuaVM. RegisterFunction (LuaFunctionName, pLuaAPIClass, mInfo );
}
}
}
/// <Summary>
/// Execute the lua script file
/// </Summary>
/// <Param name = "luaFileName"> script file name </param>
Public void ExecuteFile (string luaFileName)
{
PLuaVM. DoFile (luaFileName );
}
/// <Summary>
/// Execute the lua script
/// </Summary>
/// <Param name = "luaCommand"> lua command </param>
Public void ExecuteString (string luaCommand)
{
PLuaVM. DoString (luaCommand );
}
}
}
Next, construct your own application and LUA's API class:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Net;
Using System. IO;
Namespace LUATest
{
Class LUAAPI
{
FrmMain fm = (frmMain) frmMain. ActiveForm;
/// <Summary>
/// Echo Method Used in LUA to add the execution result msg of LUA to ListBox txtResult
/// </Summary>
/// <Param name = "msg"> execution result </param>
[Lua_Cls ("Echo")]
Public void Echo (string msg)
{
Fm. EchoResult (msg );
}}
}
Next, let's change our form class:
Public partial class frmMain: Form
{
Private string FileName;
Public frmMain ()
{
InitializeComponent ();
}
Private void btnFileName_Click (object sender, EventArgs e)
{
OpenFileDialog ofg = new OpenFileDialog ();
Ofg. Filter = "LUA script file (*. lua) | *. lua | all files (*. *) | *.*";
If (ofg. ShowDialog () = DialogResult. OK)
{
TxtFileName. Text = ofg. FileName;
FileName = ofg. FileName;
StreamReader sr = new StreamReader (ofg. FileName, Encoding. Default );
TxtFileContent. Text = sr. ReadToEnd ();
Sr. Close ();
}
}
Public void EchoResult (string Msg)
{
TxtResult. Items. Add (Msg );
}
Private void btnRun_Click (object sender, EventArgs e)
{
LUAFrame lf = new LUAFrame ();
Lf. BindLuaApiClass (new LUAAPI ());
Lf. ExecuteFile (FileName );
}
}
The content of the LUA file remains unchanged, so that I can get the same output result as I did.
From Mr_Lonely