ILHelper.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Reflection;
  3. using ILRuntime.Runtime.Enviorment;
  4. namespace Model
  5. {
  6. public static class ILHelper
  7. {
  8. public static unsafe void InitILRuntime()
  9. {
  10. // 注册重定向函数
  11. MethodInfo mi = typeof(Log).GetMethod("Debug", new Type[] { typeof(string) });
  12. Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
  13. MethodInfo mi2 = typeof(Log).GetMethod("Info", new Type[] { typeof(string) });
  14. Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi2, ILRedirection.LogInfo);
  15. MethodInfo mi3 = typeof(Log).GetMethod("Error", new Type[] { typeof(string) });
  16. Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi3, ILRedirection.LogError);
  17. // 注册委托
  18. Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
  19. Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
  20. // 注册适配器
  21. Assembly assembly = typeof(Init).Assembly;
  22. foreach (Type type in assembly.GetTypes())
  23. {
  24. object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
  25. if (attrs.Length == 0)
  26. {
  27. continue;
  28. }
  29. object obj = Activator.CreateInstance(type);
  30. CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
  31. if (adaptor == null)
  32. {
  33. continue;
  34. }
  35. Init.Instance.AppDomain.RegisterCrossBindingAdaptor(adaptor);
  36. }
  37. }
  38. }
  39. }