public TracingDiagnosticMethod(ITracingDiagnosticProcessor TracingDiagnosticProcessor, MethodInfo MethodInfo, String DiagnosticName) { _TracingDiagnosticProcessor = TracingDiagnosticProcessor; _Reflector = MethodInfo.GetReflector(); _DiagnosticName = DiagnosticName; _RarameterResolvers = GetParameterResolvers(MethodInfo).ToArray(); }
protected virtual Task BeforeAsync(MethodReflector targetMethod, object[] args) { #if DEBUG IsBefore = true; #endif return(Task.CompletedTask); }
protected virtual Task BeforeAsync(MethodReflector targetMethod, object[] args) { #if NETSTANDARD return(Task.CompletedTask); #else return(Task.FromResult <object>(null)); #endif }
public TracingDiagnosticMethod(ITracingDiagnosticProcessor tracingDiagnosticProcessor, MethodInfo method, string diagnosticName) { _tracingDiagnosticProcessor = tracingDiagnosticProcessor; _reflector = method.GetReflector(); _diagnosticName = diagnosticName; _parameterResolvers = GetParameterResolvers(method).ToArray(); }
public DiagnosticMethodSubscription(IDiagnosticProcessor diagnosticProcessor, MethodInfo method, string diagnosticName) { _DiagnosticProcessor = diagnosticProcessor; _reflector = method.GetReflector(); _diagnosticName = diagnosticName; _parameterResolvers = GetParameterResolvers(method).ToArray(); }
public virtual Task AfterAsync(MethodReflector method, Exception exception, object instance, params object[] param) { Console.WriteLine($"AfterAsync Name:{method.Name}"); #if NETSTANDARD return(Task.CompletedTask); #else return(Task.FromResult <object>(null)); #endif }
private Expression GetEntitiesAsParameterExpr(Type entityType) { var getEntitiesMi = MethodReflector.Get <EntityManager, Type>((em, t) => em.GetEntities(t)); var entityTypeExpr = Expression.Constant(entityType, typeof(Type)); var egExpr = Expression.Call(_entityManagerParameterExpr, getEntitiesMi, entityTypeExpr); var methExpr = Expression.Call(typeof(Enumerable), "Cast", new Type[] { entityType }, egExpr); return(methExpr); }
public async Task <object> ExecuteAsync(MethodReflector targetMethod, object instance, object[] args) { object result = null; await BeforeAsync(targetMethod, args); result = targetMethod.Invoke(instance, args); await AfterAsync(targetMethod, args); return(result); }
private static HttpResponseMessageSettingsAttribute[] GetResponseSetters(MethodReflector mr, TypeReflector tr) { return(mr.GetCustomAttributes <HttpResponseMessageSettingsAttribute>() .Union(mr.Parameters.SelectMany(i => i.GetCustomAttributes <ParameterResponseMessageSettingsAttribute>() .Select(j => { j.Parameter = i.MemberInfo; return j; }))) .Union(tr.GetCustomAttributes <HttpResponseMessageSettingsAttribute>()) .OrderBy(i => i.Order) .ToArray()); }
public MethodReflectorBenchmarks() { var typeInfo = typeof(MethodFakes).GetTypeInfo(); _method = typeInfo.GetMethod("Call"); _staticMethod = typeInfo.GetMethod("StaticCall"); _virtualMethod = typeInfo.GetMethod("CallVirt"); _staticReflector = _staticMethod.GetReflector(); _callReflectorWithCallOp = _method.GetReflector(CallOptions.Call); _callReflectorWithCallVirtOp = _method.GetReflector(CallOptions.Callvirt); _virtualReflectorWithCallOp = _virtualMethod.GetReflector(CallOptions.Call); _virtualReflectorWithCallVirtOp = _virtualMethod.GetReflector(CallOptions.Callvirt); _instance = new MethodFakes(); }
private static IHttpRequestMessageSettings[] GetRequestSetters(MethodReflector mr, TypeReflector tr) { return(mr.GetCustomAttributesDistinctBy <HttpMethodAttribute>(tr) .Select(i => i.CreateSettings(mr.Parameters.Where(j => j.IsDefined <RouteAttribute>()), mr.Parameters.Where(j => j.IsDefined <QueryAttribute>()))) .First() .Union(mr.Parameters.SelectMany(i => i.GetCustomAttributes <ParameterRequestMessageSettingsAttribute>() .Select(j => { j.Parameter = i.MemberInfo; return j; }))) .Union(tr.GetCustomAttributes <HttpRequestMessageSettingsAttribute>()) .Union(mr.GetCustomAttributes <HttpRequestMessageSettingsAttribute>()) .OrderBy(i => i.Order) .ToArray()); }
private ServiceEntry Create(MethodInfo method, MethodReflector implementationMethod) { var serviceId = _serviceIdGenerator.GenerateServiceId(method); var serviceDescriptor = new ServiceDescriptor { Id = serviceId }; var descriptorAttributes = method.GetCustomAttributes <RpcServiceDescriptorAttribute>(); foreach (var descriptorAttribute in descriptorAttributes) { descriptorAttribute.Apply(serviceDescriptor); } return(new ServiceEntry { Descriptor = serviceDescriptor, Func = parameters => { var serviceScopeFactory = _serviceProvider.GetRequiredService <IServiceScopeFactory>(); using (var scope = serviceScopeFactory.CreateScope()) { var instance = scope.ServiceProvider.GetRequiredService(method.DeclaringType); var list = new List <object>(); foreach (var parameterInfo in method.GetParameters()) { if (parameterInfo.HasDefaultValue && !parameters.ContainsKey(parameterInfo.Name)) { list.Add(parameterInfo.DefaultValue); continue; } var value = parameters[parameterInfo.Name]; var parameterType = parameterInfo.ParameterType; var parameter = _typeConvertibleService.Convert(value, parameterType); list.Add(parameter); } var result = implementationMethod.Invoke(instance, list.ToArray()); return Task.FromResult(result); } } }); }
/// <summary> /// 调用切面的拦截方法 /// </summary> /// <param name="instance"></param> /// <param name="methodInfo"></param> /// <param name="parameters"></param> /// <param name="context"></param> /// <param name="invocation"></param> /// <param name="_next"></param> /// <param name="returnValue"></param> /// <param name="returnParam"></param> /// <param name="injectAnotation"></param> /// <returns></returns> public static object InvokeInstanceMethod(object instance, MethodReflector methodInfo, ParameterInfo[] parameters, IComponentContext context, AspectContext invocation = null, AspectDelegate _next = null, object returnValue = null, string returnParam = null, Attribute injectAnotation = null) { if (parameters == null || parameters.Length == 0) { return(methodInfo.Invoke(instance, null)); } //自动类型注入 List <object> parameterObj = new List <object>(); foreach (var parameter in parameters) { if (invocation != null && parameter.ParameterType == typeof(AspectContext)) { parameterObj.Add(invocation); continue; } if (_next != null && parameter.ParameterType == typeof(AspectDelegate)) { parameterObj.Add(_next); continue; } if (injectAnotation != null && parameter.ParameterType == injectAnotation.GetType()) { parameterObj.Add(injectAnotation); continue; } if (returnValue != null && !string.IsNullOrWhiteSpace(returnParam) && parameter.Name.Equals(returnParam)) { //如果指定的类型会出错 parameterObj.Add(returnValue); continue; } var autowired = parameter.GetCustomAttribute <Autowired>(); if (autowired != null) { parameterObj.Add(autowired.ResolveParameter(parameter, context)); continue; } var value = parameter.GetCustomAttribute <Value>(); if (value != null) { parameterObj.Add(value.ResolveParameter(parameter, context)); continue; } if (parameter.HasDefaultValue) { parameterObj.Add(parameter.RawDefaultValue); continue; } if (parameter.IsOptional) { parameterObj.Add(Type.Missing); continue; } if (parameter.IsOut) { parameterObj.Add(Type.Missing); continue; } if (parameter.ParameterType.IsValueType || parameter.ParameterType.IsEnum) { parameterObj.Add(parameter.RawDefaultValue); continue; } //如果拿不到就默认 context.TryResolve(parameter.ParameterType, out var obj); parameterObj.Add(obj); } return(methodInfo.Invoke(instance, parameterObj.ToArray())); }
public DiagnosticEvent(IDiagnosticProcessor diagnosticProcessor, MethodInfo method) { _diagnosticProcessor = diagnosticProcessor; _reflector = method.GetReflector(); _parameterResolvers = GetParameterResolvers(method).ToArray(); }
public override IAsyncPolicy BuildAsync(MethodReflector method) { return(Policy.TimeoutAsync(timeSpan, TimeoutStrategy.Optimistic)); }
public override IAsyncPolicy BuildAsync(MethodReflector method) { return(Policy.BulkheadAsync(maxParallelization, maxQueuingActions)); }
public override IAsyncPolicy BuildAsync(MethodReflector method) { return(PolicyExtensions.CreatePolicyBuilder(ExceptionType) .AdvancedCircuitBreakerAsync(failureThreshold, samplingDuration, minimumThroughput, durationOfBreak)); }
public virtual void After(MethodReflector method, Exception exception, object instance, params object[] param) { Console.WriteLine($"After Name:{method.Name}"); }
public override bool CanAspect(MethodReflector method) { return(method.IsDefined <HttpMethodAttribute>()); }
public override bool CanAspect(MethodReflector method) { return(method.Parameters.Any(i => i.IsDefined <InjectAttribute>())); }
public override bool CanAspect(MethodReflector method) { return(method.IsDefined <AbstractPolicyAttribute>()); }
public abstract ISyncPolicy Build(MethodReflector method);
protected override Task AfterAsync(MethodReflector targetMethod, object[] args) { Console.WriteLine($"AfterAsync {targetMethod.Name}"); return(base.AfterAsync(targetMethod, args)); }
public abstract IAsyncPolicy BuildAsync(MethodReflector method);
public virtual Task BeforeAsync(MethodReflector method, object instance, params object[] param) { Console.WriteLine($"BeforeAsync Name:{method.Name}"); return(Task.CompletedTask); }
public virtual Task AfterAsync(MethodReflector method, Exception exception, object instance, params object[] param) { Console.WriteLine($"AfterAsync Name:{method.Name}"); return(Task.CompletedTask); }
public override IAsyncPolicy BuildAsync(MethodReflector method) { return(PolicyExtensions.CreatePolicyBuilder(ExceptionType).RetryAsync(retryCount)); }
public override bool CanAspect(MethodReflector method) { return(method.IsDefined <CacheAttribute>()); // 限制只对有缓存定义的方法起效 }
public override IAsyncPolicy BuildAsync(MethodReflector method) { return(PolicyExtensions.CreatePolicyBuilder(ExceptionType) .CircuitBreakerAsync(exceptionsAllowedBeforeBreaking, durationOfBreak)); }
public virtual void Before(MethodReflector method, object instance, params object[] param) { Console.WriteLine($"Before Name:{method.Name}"); }