ReflectorExtensions.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using AspectCore.Extensions.Reflection;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Reflection.Metadata;
  10. using TEAMModelOS.Filter;
  11. using TEAMModelOS.SDK.Context.Attributes.Azure;
  12. using TEAMModelOS.SDK.Models;
  13. namespace TEAMModelOS.SDK.Helper.Common.ReflectorExtensions
  14. {
  15. public static class ReflectorExtensions
  16. {
  17. public static T GetPropertyValue<T>(this object obj, string name, BindingFlags bindingFlags) where T : class
  18. {
  19. var chProperty = obj.GetType().GetTypeInfo().GetProperty(name, bindingFlags);
  20. var chReflector = chProperty.GetReflector();
  21. var value = chReflector.GetValue(obj) as T;
  22. return value;
  23. }
  24. public static Tuple<T, FieldInfo> GetFieldValue<T>(this object obj, string name, BindingFlags bindingFlags) where T : class
  25. {
  26. var chField = obj.GetType().GetTypeInfo().GetField(name, bindingFlags);
  27. var chReflector = chField.GetReflector();
  28. var value = chReflector.GetValue(obj) as T;
  29. return Tuple.Create(value, chField);
  30. }
  31. public static T GetFieldValue<T>(this Type type, string name, BindingFlags bindingFlags) where T : class
  32. {
  33. var chField = type.GetTypeInfo().GetField(name, bindingFlags);
  34. var chReflector = chField.GetReflector();
  35. var value = chReflector.GetValue(null) as T;
  36. return value;
  37. }
  38. /// <summary>
  39. /// 获取T类型的属性标记的类的方法集合
  40. /// </summary>
  41. /// <typeparam name="T"></typeparam>
  42. /// <returns></returns>
  43. public static List<Attribute> GetMethodCustomAttribute<T>(string[] ScanModel)
  44. {
  45. Type attr = typeof(T);
  46. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  47. List<Attribute> attributes = new List<Attribute>();
  48. if (ScanModel != null)
  49. {
  50. foreach (var model in ScanModel)
  51. {
  52. Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
  53. var TypeInModel = assembly.GetTypes().Select(x => x.GetMethods()).SelectMany(y => y).Select(z=>z.GetCustomAttribute(attr,true)).Where(n=>n!=null);
  54. attributes.AddRange(TypeInModel);
  55. }
  56. }
  57. return attributes;
  58. }
  59. /// <summary>
  60. /// 获取T类型的属性标记的类的方法集合
  61. /// </summary>
  62. /// <typeparam name="T"></typeparam>
  63. /// <returns></returns>
  64. public static (List<OpenApi>,List<ApiTokenAttribute>) GetMethodCustomAttribute<T,V>(string[] ScanModel)
  65. {
  66. List<OpenApi> openApis = new List<OpenApi>();
  67. Type attrT = typeof(T);
  68. Type attrV = typeof(V);
  69. string currentDirectory = Path.GetDirectoryName(attrT.Assembly.Location);
  70. List<ApiTokenAttribute> attributes = new List<ApiTokenAttribute>();
  71. if (ScanModel != null)
  72. {
  73. foreach (var model in ScanModel)
  74. {
  75. Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
  76. var TypeInModelS = assembly.GetTypes().SelectMany(x => x.GetMethods()).GroupBy(z => z.Name).ToList();
  77. // var TypeInModelSS = assembly.GetTypes().SelectMany(x => x.GetMethods()).Where(x=>);
  78. 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());
  79. Type attrR = typeof(RouteAttribute);
  80. foreach (var item in TypeInModelTV) {
  81. var routeAttr = item.ReflectedType.GetCustomAttribute<RouteAttribute>();
  82. var postAttr = item.GetCustomAttribute(attrV,true);
  83. var apiAttr = item.GetCustomAttribute(attrT, true);
  84. if (routeAttr!=null && postAttr!=null && apiAttr!=null) {
  85. ApiTokenAttribute apiTokenAttribute = (ApiTokenAttribute)apiAttr;
  86. HttpPostAttribute httpPostAttribute = (HttpPostAttribute)postAttr;
  87. OpenApi openApi = new OpenApi
  88. {
  89. PartitionKey = "IES5-API",
  90. RowKey = apiTokenAttribute.Auth,
  91. auth = int.Parse(apiTokenAttribute.Auth),
  92. method = "POST",
  93. name = apiTokenAttribute.Name,
  94. type= apiTokenAttribute.RW,
  95. url =$"/{routeAttr.Template}/{httpPostAttribute.Template}"
  96. };
  97. openApis.Add(openApi);
  98. attributes.Add(apiTokenAttribute);
  99. }
  100. }
  101. }
  102. }
  103. return (openApis, attributes) ;
  104. }
  105. /// <summary>
  106. /// 获取T类型属性标记的类集合
  107. /// </summary>
  108. /// <typeparam name="T"></typeparam>
  109. /// <returns></returns>
  110. public static List<Type> GetAllTypeAsAttribute<T>(string[] ScanModel)
  111. {
  112. Type attr = typeof(T);
  113. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  114. List<Type> types = new List<Type>();
  115. if (ScanModel != null)
  116. {
  117. foreach (var model in ScanModel)
  118. {
  119. Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
  120. // Assembly assembly = Assembly.GetCallingAssembly();
  121. var TypeInModel = from t in assembly.GetTypes()
  122. let attributes = t.GetCustomAttributes(attr, true)
  123. where attributes != null && attributes.Length > 0
  124. select t;
  125. types.AddRange(TypeInModel);
  126. }
  127. }
  128. return types;
  129. }
  130. }
  131. }