ReflectorExtensions.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using AspectCore.Extensions.Reflection;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Azure.Functions.Worker;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Reflection.Metadata;
  11. using TEAMModelOS.Filter;
  12. using TEAMModelOS.SDK.Context.Attributes.Azure;
  13. using TEAMModelOS.SDK.Models;
  14. namespace TEAMModelOS.SDK.Helper.Common.ReflectorExtensions
  15. {
  16. public static class ReflectorExtensions
  17. {
  18. public static T GetPropertyValue<T>(this object obj, string name, BindingFlags bindingFlags) where T : class
  19. {
  20. var chProperty = obj.GetType().GetTypeInfo().GetProperty(name, bindingFlags);
  21. var chReflector = chProperty.GetReflector();
  22. var value = chReflector.GetValue(obj) as T;
  23. return value;
  24. }
  25. public static Tuple<T, FieldInfo> GetFieldValue<T>(this object obj, string name, BindingFlags bindingFlags) where T : class
  26. {
  27. var chField = obj.GetType().GetTypeInfo().GetField(name, bindingFlags);
  28. var chReflector = chField.GetReflector();
  29. var value = chReflector.GetValue(obj) as T;
  30. return Tuple.Create(value, chField);
  31. }
  32. public static T GetFieldValue<T>(this Type type, string name, BindingFlags bindingFlags) where T : class
  33. {
  34. var chField = type.GetTypeInfo().GetField(name, bindingFlags);
  35. var chReflector = chField.GetReflector();
  36. var value = chReflector.GetValue(null) as T;
  37. return value;
  38. }
  39. /// <summary>
  40. /// 获取T类型的属性标记的类的方法集合
  41. /// </summary>
  42. /// <typeparam name="T"></typeparam>
  43. /// <returns></returns>
  44. public static List<Attribute> GetMethodCustomAttribute<T>(string[] ScanModel)
  45. {
  46. Type attr = typeof(T);
  47. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  48. List<Attribute> attributes = new List<Attribute>();
  49. if (ScanModel != null)
  50. {
  51. foreach (var model in ScanModel)
  52. {
  53. Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
  54. var TypeInModel = assembly.GetTypes().Select(x => x.GetMethods()).SelectMany(y => y).Select(z=>z.GetCustomAttribute(attr,true)).Where(n=>n!=null);
  55. attributes.AddRange(TypeInModel);
  56. }
  57. }
  58. return attributes;
  59. }
  60. /// <summary>
  61. /// 获取T类型的属性标记的类的方法集合
  62. /// </summary>
  63. /// <typeparam name="T"></typeparam>
  64. /// <returns></returns>
  65. public static (List<WebHook>, List<ApiTokenAttribute>) GetWebHook(string[] ScanModel)
  66. {
  67. List<WebHook> webHooks = new List<WebHook>();
  68. Type attrT = typeof(ApiTokenAttribute);
  69. Type attrV = typeof(FunctionAttribute);
  70. string currentDirectory = Path.GetDirectoryName(attrT.Assembly.Location);
  71. List<ApiTokenAttribute> attributes = new List<ApiTokenAttribute>();
  72. if (ScanModel != null)
  73. {
  74. foreach (var model in ScanModel)
  75. {
  76. Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
  77. var TypeInModelTV = assembly.GetTypes().SelectMany(x => x.GetMethods()).Where(m => m.GetCustomAttributes().Where(t => t.GetType().Equals(attrT)).Any() && m.GetCustomAttributes().Where(t => t.GetType().Equals(attrV)).Any());
  78. foreach (var item in TypeInModelTV)
  79. {
  80. var funcAttr = item.GetCustomAttribute(attrV, true);
  81. var apiAttr = item.GetCustomAttribute(attrT, true);
  82. ApiTokenAttribute apiTokenAttribute = (ApiTokenAttribute)apiAttr;
  83. FunctionAttribute functionAttribute = (FunctionAttribute)funcAttr;
  84. WebHook webHook = new WebHook
  85. {
  86. PartitionKey = "IES5-WEBHOOK",
  87. RowKey = apiTokenAttribute.Auth,
  88. auth = int.Parse(apiTokenAttribute.Auth),
  89. method = "POST",
  90. name = apiTokenAttribute.Name,
  91. tname = apiTokenAttribute.TName,
  92. ename = apiTokenAttribute.EName,
  93. type = apiTokenAttribute.RWN,
  94. notice = functionAttribute.Name,
  95. url= "/webhook"
  96. };
  97. webHooks.Add(webHook);
  98. attributes.Add(apiTokenAttribute);
  99. }
  100. }
  101. }
  102. return (webHooks, attributes);
  103. }
  104. /// <summary>
  105. /// 获取T类型的属性标记的类的方法集合
  106. /// </summary>
  107. /// <typeparam name="T"></typeparam>
  108. /// <returns></returns>
  109. public static (List<OpenApi>,List<ApiTokenAttribute>) GetOpenApi(string[] ScanModel)
  110. {
  111. List<OpenApi> openApis = new List<OpenApi>();
  112. Type attrT = typeof(ApiTokenAttribute);
  113. Type attrV = typeof(HttpPostAttribute);
  114. string currentDirectory = Path.GetDirectoryName(attrT.Assembly.Location);
  115. List<ApiTokenAttribute> attributes = new List<ApiTokenAttribute>();
  116. if (ScanModel != null)
  117. {
  118. foreach (var model in ScanModel)
  119. {
  120. Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
  121. var TypeInModelTV = assembly.GetTypes().SelectMany(x => x.GetMethods()).Where(m=>m.GetCustomAttributes().Where(t=>t.GetType().Equals(attrT)).Any() && m.GetCustomAttributes().Where(t => t.GetType().Equals(attrV)).Any());
  122. foreach (var item in TypeInModelTV) {
  123. var routeAttr = item.ReflectedType.GetCustomAttribute<RouteAttribute>();
  124. var postAttr = item.GetCustomAttribute(attrV,true);
  125. var apiAttr = item.GetCustomAttribute(attrT, true);
  126. if (routeAttr!=null && postAttr!=null && apiAttr!=null) {
  127. ApiTokenAttribute apiTokenAttribute = (ApiTokenAttribute)apiAttr;
  128. HttpPostAttribute httpPostAttribute = (HttpPostAttribute)postAttr;
  129. OpenApi openApi = new OpenApi
  130. {
  131. PartitionKey = $"IES5-API-{routeAttr.Template}",
  132. RowKey = apiTokenAttribute.Auth,
  133. auth = int.Parse(apiTokenAttribute.Auth),
  134. method = "POST",
  135. name = apiTokenAttribute.Name,
  136. type= apiTokenAttribute.RWN,
  137. ename = apiTokenAttribute.EName,
  138. tname = apiTokenAttribute.TName,
  139. url =$"/{routeAttr.Template}/{httpPostAttribute.Template}"
  140. };
  141. openApis.Add(openApi);
  142. attributes.Add(apiTokenAttribute);
  143. }
  144. }
  145. }
  146. }
  147. return (openApis, attributes) ;
  148. }
  149. /// <summary>
  150. /// 获取T类型属性标记的类集合
  151. /// </summary>
  152. /// <typeparam name="T"></typeparam>
  153. /// <returns></returns>
  154. public static List<Type> GetAllTypeAsAttribute<T>(string[] ScanModel)
  155. {
  156. Type attr = typeof(T);
  157. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  158. List<Type> types = new List<Type>();
  159. if (ScanModel != null)
  160. {
  161. foreach (var model in ScanModel)
  162. {
  163. Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
  164. // Assembly assembly = Assembly.GetCallingAssembly();
  165. var TypeInModel = from t in assembly.GetTypes()
  166. let attributes = t.GetCustomAttributes(attr, true)
  167. where attributes != null && attributes.Length > 0
  168. select t;
  169. types.AddRange(TypeInModel);
  170. }
  171. }
  172. return types;
  173. }
  174. }
  175. }