TestReflection.cs

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using LuaInterface;
  4. using System;
  5. using System.Reflection;
  6. public class TestReflection : LuaClient
  7. {
  8. string script =
  9. @"
  10. require 'tolua.reflection'
  11. tolua.loadassembly('Assembly-CSharp')
  12. tolua.loadassembly('mscorlib')
  13. local BindingFlags = require 'System.Reflection.BindingFlags'
  14. function DoClick()
  15. print('do click')
  16. end
  17. function Test()
  18. local t = typeof('TestExport')
  19. local func = tolua.getmethod(t, 'TestReflection')
  20. func:Call()
  21. func:Destroy()
  22. func = nil
  23. local objs = {Vector3.one, Vector3.zero}
  24. local array = tolua.toarray(objs, typeof(Vector3))
  25. local obj = tolua.createinstance(t, array)
  26. --local constructor = tolua.getconstructor(t, typeof(Vector3):MakeArrayType())
  27. --local obj = constructor:Call(array)
  28. --constructor:Destroy()
  29. func = tolua.getmethod(t, 'Test', typeof('System.Int32'):MakeByRefType())
  30. local r, o = func:Call(obj, 123)
  31. print(r..':'..o)
  32. func:Destroy()
  33. local property = tolua.getproperty(t, 'Number')
  34. local num = property:Get(obj, null)
  35. print('object Number: '..num)
  36. property:Set(obj, 456, null)
  37. num = property:Get(obj, null)
  38. property:Destroy()
  39. print('object Number: '..num)
  40. local field = tolua.getfield(t, 'field')
  41. num = field:Get(obj)
  42. print('object field: '.. num)
  43. field:Set(obj, 2048)
  44. num = field:Get(obj)
  45. field:Destroy()
  46. print('object field: '.. num)
  47. field = tolua.getfield(t, 'OnClick')
  48. local onClick = field:Get(obj)
  49. onClick = onClick + DoClick
  50. field:Set(obj, onClick)
  51. local click = field:Get(obj)
  52. click:DynamicInvoke()
  53. field:Destroy()
  54. click:Destroy()
  55. end
  56. ";
  57. string tips = null;
  58. protected override LuaFileUtils InitLoader()
  59. {
  60. #if UNITY_5
  61. Application.logMessageReceived += ShowTips;
  62. #else
  63. Application.RegisterLogCallback(ShowTips);
  64. #endif
  65. return new LuaResLoader();
  66. }
  67. //屏蔽,例子不需要运行
  68. protected override void CallMain() { }
  69. void TestAction()
  70. {
  71. Debugger.Log("Test Action");
  72. }
  73. protected override void OnLoadFinished()
  74. {
  75. base.OnLoadFinished();
  76. /*Type t = typeof(TestExport);
  77. MethodInfo md = t.GetMethod("TestReflection");
  78. md.Invoke(null, null);
  79. Vector3[] array = new Vector3[] { Vector3.zero, Vector3.one };
  80. object obj = Activator.CreateInstance(t, array);
  81. md = t.GetMethod("Test", new Type[] { typeof(int).MakeByRefType() });
  82. object o = 123;
  83. object[] args = new object[] { o };
  84. object ret = md.Invoke(obj, args);
  85. Debugger.Log(ret + " : " + args[0]);
  86. PropertyInfo p = t.GetProperty("Number");
  87. int num = (int)p.GetValue(obj, null);
  88. Debugger.Log("object Number: {0}", num);
  89. p.SetValue(obj, 456, null);
  90. num = (int)p.GetValue(obj, null);
  91. Debugger.Log("object Number: {0}", num);
  92. FieldInfo f = t.GetField("field");
  93. num = (int)f.GetValue(obj);
  94. Debugger.Log("object field: {0}", num);
  95. f.SetValue(obj, 2048);
  96. num = (int)f.GetValue(obj);
  97. Debugger.Log("object field: {0}", num);*/
  98. luaState.CheckTop();
  99. luaState.DoString(script, "TestReflection.cs");
  100. LuaFunction func = luaState.GetFunction("Test");
  101. func.Call();
  102. func.Dispose();
  103. func = null;
  104. }
  105. void ShowTips(string msg, string stackTrace, LogType type)
  106. {
  107. tips += msg;
  108. tips += "\r\n";
  109. }
  110. new void OnApplicationQuit()
  111. {
  112. #if UNITY_5
  113. Application.logMessageReceived += ShowTips;
  114. #else
  115. Application.RegisterLogCallback(ShowTips);
  116. #endif
  117. Destroy();
  118. }
  119. void OnGUI()
  120. {
  121. GUI.Label(new Rect(Screen.width / 2 - 250, Screen.height / 2 - 150, 500, 300), tips);
  122. }
  123. }

?