using AspectCore.Extensions.Reflection; using Microsoft.AspNetCore.Hosting; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Reflection.Metadata; using TEAMModelOS.SDK.Context.Attributes.Azure; namespace TEAMModelOS.SDK.Helper.Common.ReflectorExtensions { public static class ReflectorExtensions { public static T GetPropertyValue(this object obj, string name, BindingFlags bindingFlags) where T : class { var chProperty = obj.GetType().GetTypeInfo().GetProperty(name, bindingFlags); var chReflector = chProperty.GetReflector(); var value = chReflector.GetValue(obj) as T; return value; } public static Tuple GetFieldValue(this object obj, string name, BindingFlags bindingFlags) where T : class { var chField = obj.GetType().GetTypeInfo().GetField(name, bindingFlags); var chReflector = chField.GetReflector(); var value = chReflector.GetValue(obj) as T; return Tuple.Create(value, chField); } public static T GetFieldValue(this Type type, string name, BindingFlags bindingFlags) where T : class { var chField = type.GetTypeInfo().GetField(name, bindingFlags); var chReflector = chField.GetReflector(); var value = chReflector.GetValue(null) as T; return value; } /// /// 获取T类型的属性标记的类的方法集合 /// /// /// public static List GetMethodCustomAttribute(string[] ScanModel) { Type attr = typeof(T); string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location); List attributes = new List(); if (ScanModel != null) { foreach (var model in ScanModel) { Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll"); var TypeInModelS = assembly.GetTypes().SelectMany(x => x.GetMethods()).GroupBy(z=>z.Name).ToList(); TypeInModelS.ForEach(x => { if (x.Key.Equals("GetTeacherInfo")) { var at = x.ToList().SelectMany(z => z.GetCustomAttributes()).Where(m=>m.GetType().Equals(attr)); string ke = x.Key; } }); var TypeInModel = assembly.GetTypes().Select(x => x.GetMethods()).SelectMany(y => y).Select(z=>z.GetCustomAttribute(attr,true)).Where(n=>n!=null); attributes.AddRange(TypeInModel); } } return attributes; } /// /// 获取T类型的属性标记的类的方法集合 /// /// /// public static List GetMethodCustomAttribute(string[] ScanModel) { Type attrT = typeof(T); Type attrV = typeof(V); string currentDirectory = Path.GetDirectoryName(attrT.Assembly.Location); List attributes = new List(); if (ScanModel != null) { foreach (var model in ScanModel) { Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll"); var TypeInModelS = assembly.GetTypes().SelectMany(x => x.GetMethods()).GroupBy(z => z.Name).ToList(); // var TypeInModelSS = assembly.GetTypes().SelectMany(x => x.GetMethods()).Where(x=>); TypeInModelS.ForEach(x => { if (x.Key.Equals("GetTeacherInfo")) { var at = x.ToList().SelectMany(z => z.GetCustomAttributes()).Where(m => m.GetType().Equals(attrT)); string ke = x.Key; } }); var TypeInModel = assembly.GetTypes().Select(x => x.GetMethods()).SelectMany(y => y).Select(z => z.GetCustomAttribute(attrT, true)).Where(n => n != null); attributes.AddRange(TypeInModel); } } return attributes; } /// /// 获取T类型属性标记的类集合 /// /// /// public static List GetAllTypeAsAttribute(string[] ScanModel) { Type attr = typeof(T); string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location); List types = new List(); if (ScanModel != null) { foreach (var model in ScanModel) { Assembly assembly = Assembly.LoadFrom(currentDirectory + "\\" + model + ".dll"); // Assembly assembly = Assembly.GetCallingAssembly(); var TypeInModel = from t in assembly.GetTypes() let attributes = t.GetCustomAttributes(attr, true) where attributes != null && attributes.Length > 0 select t; types.AddRange(TypeInModel); } } return types; } } }