No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReflectionUtil.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace XCharts.Runtime
  7. {
  8. public static class ReflectionUtil
  9. {
  10. private static Dictionary<object, MethodInfo> listClearMethodInfoCaches = new Dictionary<object, MethodInfo>();
  11. private static Dictionary<object, MethodInfo> listAddMethodInfoCaches = new Dictionary<object, MethodInfo>();
  12. public static void InvokeListClear(object obj, FieldInfo field)
  13. {
  14. var list = field.GetValue(obj);
  15. MethodInfo method;
  16. if (!listClearMethodInfoCaches.TryGetValue(list, out method))
  17. {
  18. method = list.GetType().GetMethod("Clear");
  19. listClearMethodInfoCaches[list] = method;
  20. }
  21. method.Invoke(list, new object[] { });
  22. }
  23. public static int InvokeListCount(object obj, FieldInfo field)
  24. {
  25. var list = field.GetValue(obj);
  26. return (int) list.GetType().GetProperty("Count").GetValue(list, null);
  27. }
  28. public static void InvokeListAdd(object obj, FieldInfo field, object item)
  29. {
  30. var list = field.GetValue(obj);
  31. MethodInfo method;
  32. if (!listAddMethodInfoCaches.TryGetValue(list, out method))
  33. {
  34. method = list.GetType().GetMethod("Add");
  35. listAddMethodInfoCaches[list] = method;
  36. }
  37. method.Invoke(list, new object[] { item });
  38. }
  39. public static T InvokeListGet<T>(object obj, FieldInfo field, int i)
  40. {
  41. var list = field.GetValue(obj);
  42. var item = list.GetType().GetProperty("Item").GetValue(list, new object[] { i });
  43. return (T) item;
  44. }
  45. public static void InvokeListAddTo<T>(object obj, FieldInfo field, Action<T> callback)
  46. {
  47. var list = field.GetValue(obj);
  48. var listType = list.GetType();
  49. var count = Convert.ToInt32(listType.GetProperty("Count").GetValue(list, null));
  50. for (int i = 0; i < count; i++)
  51. {
  52. var item = listType.GetProperty("Item").GetValue(list, new object[] { i });
  53. callback((T) item);
  54. }
  55. }
  56. public static object DeepCloneSerializeField(object obj)
  57. {
  58. if (obj == null)
  59. return null;
  60. var type = obj.GetType();
  61. if (type.IsValueType || type == typeof(string))
  62. {
  63. return obj;
  64. }
  65. else if (type.IsArray)
  66. {
  67. var elementType = Type.GetType(type.FullName.Replace("[]", string.Empty));
  68. var array = obj as Array;
  69. var copied = Array.CreateInstance(elementType, array.Length);
  70. for (int i = 0; i < array.Length; i++)
  71. copied.SetValue(DeepCloneSerializeField(array.GetValue(i)), i);
  72. return Convert.ChangeType(copied, obj.GetType());
  73. }
  74. else if (type.IsClass)
  75. {
  76. object returnObj;
  77. var listObj = obj as IList;
  78. if (listObj != null)
  79. {
  80. var properties = type.GetProperties();
  81. var customList = typeof(List<>).MakeGenericType((properties[properties.Length - 1]).PropertyType);
  82. returnObj = (IList) Activator.CreateInstance(customList);
  83. var list = (IList) returnObj;
  84. foreach (var item in ((IList) obj))
  85. {
  86. if (item == null)
  87. continue;
  88. list.Add(DeepCloneSerializeField(item));
  89. }
  90. }
  91. else
  92. {
  93. try
  94. {
  95. returnObj = Activator.CreateInstance(type);
  96. }
  97. catch
  98. {
  99. return null;
  100. }
  101. var fileds = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  102. for (int i = 0; i < fileds.Length; i++)
  103. {
  104. var field = fileds[i];
  105. if (!field.IsDefined(typeof(SerializeField), false))
  106. continue;
  107. var filedValue = field.GetValue(obj);
  108. if (filedValue == null)
  109. {
  110. field.SetValue(returnObj, filedValue);
  111. }
  112. else
  113. {
  114. field.SetValue(returnObj, DeepCloneSerializeField(filedValue));
  115. }
  116. }
  117. }
  118. return returnObj;
  119. }
  120. else
  121. {
  122. throw new ArgumentException("DeepCloneSerializeField: Unknown type:" + type + "," + obj);
  123. }
  124. }
  125. }
  126. }