ReflectorExtensions.cs 5.6 KB

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