tolua反射应用
有时候要修改紧急bug或者增加一个新功能,但又不能更新整包。C#代码没有导出wrap,可以临时用一下tolua的反射。
C#代码:
namespace MyNameSpace
{
public class ToluaReflection
{
public string StringField = "ToluaReflection";
}
}
lua代码:
function toLuaReflectionTest()
require 'tolua.reflection'
tolua.loadassembly('Assembly-CSharp')
local reflectionTest = typeof("MyNameSpace.ToluaReflection")
local instance = tolua.createinstance(reflectionTest)
local BindingFlags = System.Reflection.BindingFlags
local flag = System.Reflection.BindingFlags.GetMask( BindingFlags.Public , BindingFlags.NonPublic , BindingFlags.Static , BindingFlags.Instance)
local pubfield = tolua.getfield(reflectionTest, "StringField", flag)
local pubfieldValue = pubfield:Get(instance)
print(pubfieldValue)
pubfield:Set(instance, "changedpubfield")
pubfieldValue = pubfield:Get(instance)
print(pubfieldValue)
end
运行结果:
参考博文:https://blog.csdn.net/zoujiandong_8888/article/details/83585213