123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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<T>(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<T, FieldInfo> GetFieldValue<T>(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<T>(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;
- }
- /// <summary>
- /// 获取T类型的属性标记的类的方法集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static List<Attribute> GetMethodCustomAttribute<T>(string[] ScanModel)
- {
- Type attr = typeof(T);
- string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
- List<Attribute> attributes = new List<Attribute>();
- 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;
- }
- /// <summary>
- /// 获取T类型的属性标记的类的方法集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static (List<WebHook>, List<ApiTokenAttribute>) GetWebHook(string[] ScanModel)
- {
- List<WebHook> webHooks = new List<WebHook>();
- Type attrT = typeof(ApiTokenAttribute);
- Type attrV = typeof(FunctionAttribute);
- string currentDirectory = Path.GetDirectoryName(attrT.Assembly.Location);
- List<ApiTokenAttribute> attributes = new List<ApiTokenAttribute>();
- 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);
- }
- /// <summary>
- /// 获取T类型的属性标记的类的方法集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static (List<OpenApi>,List<ApiTokenAttribute>) GetOpenApi(string[] ScanModel)
- {
- List<OpenApi> openApis = new List<OpenApi>();
- Type attrT = typeof(ApiTokenAttribute);
- Type attrV = typeof(HttpPostAttribute);
- string currentDirectory = Path.GetDirectoryName(attrT.Assembly.Location);
- List<ApiTokenAttribute> attributes = new List<ApiTokenAttribute>();
- 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<RouteAttribute>();
- 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) ;
- }
- /// <summary>
- /// 获取T类型属性标记的类集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static List<Type> GetAllTypeAsAttribute<T>(string[] ScanModel)
- {
- Type attr = typeof(T);
- string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
- List<Type> types = new List<Type>();
- 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;
- }
- }
- }
|