uLua通过反射获取类成员
在Unity中使用ulua框架进行热更新时,有的时候需要在不更新整包的情况下增加一些小功能,而这期间会用到一些没有wrap到的Mono属性或者方法,此时可以用过调用tolua的反射获取。
C#代码:
using UnityEngine;
namespace Test
{
public class ReflectionTest
{
// 属性
public string pubfield = "pubfield";
private string prifield = "prifield";
public string Prifield
{
get
{
return prifield;
}
set
{
prifield = value;
}
}
// 方法
public string getVector3ListDesc(Vector3[] v)
{
string desc = string.Empty;
for (int i = 0; i < v.Length; i++)
{
desc += "(" + v[i].x + "," + v[i].y + "," + v[i].z + ")";
}
return desc;
}
// 静态属性
public static string stapubfield = "stapubfield";
private static string staprifield = "staprifield";
public static string Staprifield
{
get
{
return staprifield;
}
set
{
staprifield = value;
}
}
// 静态方法
public static string contactParam(string v1, int v2)
{
return v1 + v2.ToString();
}
}
}
lua代码:
---
--- Created by Administrator.
--- DateTime: 2018/10/31 11:47
---
function luaReflectionTest()
--程序集反射
require 'tolua.reflection'
tolua.loadassembly('Assembly-CSharp')
local reflectionTest = typeof("Test.ReflectionTest")
local instance = tolua.createinstance(reflectionTest)
--获取和修改pubfield的值
local pubfield = tolua.getfield(reflectionTest, "pubfield")
local pubfieldValue = pubfield:Get(instance)
print("pubfieldVale=" .. pubfieldValue)
pubfield:Set(instance, "changedpubfield")
local pubfieldValueChanged = pubfield:Get(instance)
print("pubfieldValueChanged=" .. pubfieldValueChanged)
pubfield:Destroy()
--执行属性的get 和 set 方法
local prifield = tolua.getproperty(reflectionTest, "Prifield")
local prifieldValue = prifield:Get(instance, nil)
print("prifieldValue=" .. prifieldValue)
prifield:Set(instance, "prifieldChanged", nil)
local prifieldValueChanged = prifield:Get(instance, nil)
print("prifieldValueChanged=" .. prifieldValueChanged)
prifield:Destroy()
--执行public 方法
local getVector3ListDestFun = tolua.getmethod(reflectionTest, "getVector3ListDesc", typeof(Vector3):MakeArrayType())
local params = {Vector3.New(1, 2, 3), Vector3.New(100, 101, 102), Vector3.New(201, 202, 203)}
local paramsArr = tolua.toarray(params, typeof(Vector3))
local funResult = getVector3ListDestFun:Call(instance, paramsArr)
print("funReturnResult=" .. funResult)
getVector3ListDestFun:Destroy()
--获取和修改静态属性stapubfield的值
local stapubfield = tolua.getfield(reflectionTest, "stapubfield")
local stacpubfieldValue = stapubfield:Get(nil)
print("stapubfieldValue=" .. stacpubfieldValue)
stapubfield:Set(nil, "stapubfieldChanged")
local stapubfieldValueChanged = stapubfield:Get(nil)
print("stapubfieldValueChanged=" .. stapubfieldValueChanged)
stapubfield:Destroy()
--执行静态属性的get 和 set 方法
local staprifield = tolua.getproperty(reflectionTest, "Staprifield")
local staprifieldValue = staprifield:Get(nil, nil)
print("staprifieldValue=" .. staprifieldValue)
staprifield:Set(null, "staprifieldChanged", nil)
local staprifieldValueChanged = staprifield:Get(nil, nil)
print("staprifieldValueChanged=" .. staprifieldValueChanged)
staprifield:Destroy()
--执行public static方法
local contaceParamFun = tolua.getmethod(reflectionTest, "contactParam", typeof("System.String"), typeof("System.Int32"))
local funResult1 = contaceParamFun:Call("传入的值为:", 100)
print("contaceParamFunResult=" .. funResult1)
contaceParamFun:Destroy()
end
执行结果: