using AspectCore.Extensions.Reflection; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Functions.Worker; 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) GetWebHook(string[] ScanModel) { List webHooks = new List(); Type attrT = typeof(ApiTokenAttribute); Type attrV = typeof(FunctionAttribute); 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 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()); foreach (var item in TypeInModelTV) { var funcAttr = item.GetCustomAttribute(attrV, true); var apiAttr = item.GetCustomAttribute(attrT, true); ApiTokenAttribute apiTokenAttribute = (ApiTokenAttribute)apiAttr; FunctionAttribute functionAttribute = (FunctionAttribute)funcAttr; WebHook webHook = new WebHook { PartitionKey = "IES5-WEBHOOK", RowKey = apiTokenAttribute.Auth, auth = int.Parse(apiTokenAttribute.Auth), method = "POST", name = apiTokenAttribute.Name, tname = apiTokenAttribute.TName, ename = apiTokenAttribute.EName, type = apiTokenAttribute.RWN, notice = functionAttribute.Name, url= "/webhook" }; webHooks.Add(webHook); attributes.Add(apiTokenAttribute); } } } return (webHooks, attributes); } /// /// 获取T类型的属性标记的类的方法集合 /// /// /// public static (List,List) GetOpenApi(string[] ScanModel) { List openApis = new List(); Type attrT = typeof(ApiTokenAttribute); Type attrV = typeof(HttpPostAttribute); 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 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()); 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; OpenApi openApi = new OpenApi { PartitionKey = $"IES5-API-{routeAttr.Template}", RowKey = apiTokenAttribute.Auth, auth = int.Parse(apiTokenAttribute.Auth), method = "POST", name = apiTokenAttribute.Name, type= apiTokenAttribute.RWN, ename = apiTokenAttribute.EName, tname = apiTokenAttribute.TName, 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; } } }