using AspectCore.Extensions.Reflection; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Reflection.Metadata; using TEAMModelOS.Filter; using TEAMModelOS.SDK.Context.Attributes.Azure; using TEAMModelOS.SDK.Models; 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 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,List) GetMethodCustomAttribute(string[] ScanModel) { List openApis = new List(); 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=>); 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()); Type attrR = typeof(RouteAttribute); foreach (var item in TypeInModelTV) { var routeAttr = item.ReflectedType.GetCustomAttribute(); var postAttr = item.GetCustomAttribute(attrV,true); var apiAttr = item.GetCustomAttribute(attrT, true); if (routeAttr!=null && postAttr!=null && apiAttr!=null) { ApiTokenAttribute apiTokenAttribute = (ApiTokenAttribute)apiAttr; HttpPostAttribute httpPostAttribute = (HttpPostAttribute)postAttr; if (apiTokenAttribute.Auth.Equals("1000")) { continue; } OpenApi openApi = new OpenApi { PartitionKey = "IES5-API", RowKey = apiTokenAttribute.Auth, auth = int.Parse(apiTokenAttribute.Auth), method = "POST", name = apiTokenAttribute.Name, type= apiTokenAttribute.RWN, url =$"/{routeAttr.Template}/{httpPostAttribute.Template}" }; openApis.Add(openApi); attributes.Add(apiTokenAttribute); } } } } return (openApis, 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; } } }