Пример #1
0
        public void Invoke(object value)
        {
            var args = new object[_parameterResolvers.Length];

            for (var i = 0; i < _parameterResolvers.Length; i++)
            {
                args[i] = _parameterResolvers[i].Resolve(value);
            }

            _reflector.Invoke(_diagnosticProcessor, args);
        }
Пример #2
0
        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);
        }
Пример #3
0
        public override Tuple <object, object[]> DecodeReturnValue(RequestPacket input, RpcMethodMetadata metdata)
        {
            if (metdata.RealReturnType == typeof(void))
            {
                return(Tuple.Create <object, object[]>(null, null));
            }
            var ins    = new CodedInputStream(input.Buffer);
            var result = Activator.CreateInstance(metdata.RealReturnType);

            MergeFrom.Invoke(result, ins);
            return(Tuple.Create <object, object[]>(result, null));
        }
        public void Invoke(string diagnosticName, object?value)
        {
            if (_diagnosticName != diagnosticName)
            {
                return;
            }

            var args = new object?[_parameterResolvers.Length];

            for (var i = 0; i < _parameterResolvers.Length; i++)
            {
                args[i] = _parameterResolvers[i].Resolve(value);
            }
            _reflector.Invoke(_DiagnosticProcessor, args);
        }
Пример #5
0
        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="DiagnosticName"></param>
        public void Invoke(String DiagnosticName, Object Value)
        {
            if (_DiagnosticName != DiagnosticName)
            {
                return;
            }
            var args = new Object[_RarameterResolvers.Length];

            for (int i = 0; i < args.Length; i++)
            {
                //设置参数
                args[i] = _RarameterResolvers[i].Resolve(Value);
            }
            //执行追踪的实现类
            _Reflector.Invoke(_TracingDiagnosticProcessor, args);
        }
        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);
                    }
                }
            });
        }
Пример #7
0
        /// <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()));
        }
Пример #8
0
 public void Reflector_CallVirtWithCallVirtOp()
 {
     _virtualReflectorWithCallVirtOp.Invoke(_instance);
 }
Пример #9
0
 public void Reflector_CallWithCallVirtOp()
 {
     _callReflectorWithCallVirtOp.Invoke(_instance);
 }