ReflectorExtensions.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 TypeInModel = assembly.GetTypes().Select(x => x.GetMethods()).SelectMany(y => y).Select(z=>z.GetCustomAttribute(attr,true)).Where(n=>n!=null);
  51. attributes.AddRange(TypeInModel);
  52. }
  53. }
  54. return attributes;
  55. }
  56. /// <summary>
  57. /// 获取T类型属性标记的类集合
  58. /// </summary>
  59. /// <typeparam name="T"></typeparam>
  60. /// <returns></returns>
  61. public static List<Type> GetAllTypeAsAttribute<T>(string[] ScanModel)
  62. {
  63. Type attr = typeof(T);
  64. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  65. List<Type> types = new List<Type>();
  66. if (ScanModel != null)
  67. {
  68. foreach (var model in ScanModel)
  69. {
  70. Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll");
  71. // Assembly assembly = Assembly.GetCallingAssembly();
  72. var TypeInModel = from t in assembly.GetTypes()
  73. let attributes = t.GetCustomAttributes(attr, true)
  74. where attributes != null && attributes.Length > 0
  75. select t;
  76. types.AddRange(TypeInModel);
  77. }
  78. }
  79. return types;
  80. }
  81. }
  82. }