Init.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Base;
  2. using UnityEngine;
  3. public class Init : MonoBehaviour
  4. {
  5. CLRSharp.CLRSharp_Environment env;
  6. // Use this for initialization
  7. void Start()
  8. {
  9. //创建CLRSharp环境
  10. env = new CLRSharp.CLRSharp_Environment(new LSharpLogger());
  11. //加载L#模块
  12. GameObject code = (GameObject)Resources.Load("Code/Code");
  13. byte[] assBytes = code.Get<TextAsset>("Controller.dll").bytes;
  14. byte[] mdbBytes = code.Get<TextAsset>("Controller.dll.mdb").bytes;
  15. System.IO.MemoryStream msDll = new System.IO.MemoryStream(assBytes);
  16. System.IO.MemoryStream msMdb = new System.IO.MemoryStream(mdbBytes);
  17. //env.LoadModule (msDll);//如果无符号是pdb的话,第二个参数传null
  18. //env.LoadModule(msDll, msPdb, new Mono.Cecil.Pdb.PdbReaderProvider());//Pdb
  19. env.LoadModule(msDll, msMdb, new Mono.Cecil.Mdb.MdbReaderProvider());//如果符号是Mdb格式
  20. Debug.Log("LoadModule Controller.dll done.");
  21. //step01建立一个线程上下文,用来模拟L#的线程模型,每个线程创建一个即可。
  22. CLRSharp.ThreadContext context = new CLRSharp.ThreadContext(env);
  23. Debug.Log("Create ThreadContext for L#.");
  24. //step02取得想要调用的L#类型
  25. CLRSharp.ICLRType wantType = env.GetType("Controller.Entry");//用全名称,包括命名空间
  26. Debug.Log("GetType:" + wantType.Name);
  27. Log.Debug(wantType.TypeForSystem.ToString());
  28. //和反射代码中的Type.GetType相对应
  29. //step03 静态调用
  30. //得到类型上的一个函数,第一个参数是函数名字,第二个参数是函数的参数表,这是一个没有参数的函数
  31. CLRSharp.IMethod method01 = wantType.GetMethod("Init", CLRSharp.MethodParamList.constEmpty());
  32. method01.Invoke(context, null, null);//第三个参数是object[] 参数表,这个例子不需要参数
  33. //这是个静态函数调用,对应到代码他就是HotFixCode.TestClass.Test1();
  34. ////step04 成员调用
  35. ////第二个测试程序是一个成员变量,所以先要创建实例
  36. //CLRSharp.IMethod methodctor = wantType.GetMethod(".ctor", CLRSharp.MethodParamList.constEmpty());//取得构造函数
  37. //object typeObj = methodctor.Invoke(context, null, null);//执行构造函数
  38. ////这几行的作用对应到代码就约等于 HotFixCode.TestClass typeObj =new HotFixCode.TestClass();
  39. //CLRSharp.IMethod method02 = wantType.GetMethod("Test2", CLRSharp.MethodParamList.constEmpty());
  40. //method02.Invoke(context, typeObj, null);
  41. ////这两行的作用就相当于 typeOBj.Test2();
  42. //
  43. //var list = CLRSharp.MethodParamList.Make(env.GetType(typeof(int)), env.GetType(typeof(string)));
  44. //CLRSharp.IMethod method03 = wantType.GetMethod("Test2", list);
  45. //method03.Invoke(context, typeObj, new object[] { 1234, "abcddd" });
  46. ////这两行的作用就相当于 typeOBj.Test2(1234,"abcddd");
  47. }
  48. }