GetMethod() public static method

public static GetMethod ( this type, string methodName ) : MethodInfo
type this
methodName string
return MethodInfo
Ejemplo n.º 1
0
        /// <summary>
        /// Create a factory generator.
        /// </summary>
        /// <param name="productRegistration">The component that will be activated in
        /// order to create the products of the factory.</param>
        /// <param name="delegateType">The delegate to provide as a factory.</param>
        /// <param name="parameterMapping">The parameter mapping mode to use.</param>
        public FactoryGenerator(Type delegateType, IComponentRegistration productRegistration, ParameterMapping parameterMapping)
        {
            if (productRegistration == null)
            {
                throw new ArgumentNullException("productRegistration");
            }
            Enforce.ArgumentTypeIsFunction(delegateType);

            _generator = CreateGenerator((activatorContextParam, resolveParameterArray) =>
            {
                // productRegistration, [new Parameter(name, (object)dps)]*
                var resolveParams = new Expression[] {
                    Expression.Constant(productRegistration, typeof(IComponentRegistration)),
                    Expression.NewArrayInit(typeof(Parameter), resolveParameterArray)
                };

                // c.Resolve(...)
                return(Expression.Call(
                           activatorContextParam,
                           ReflectionExtensions.GetMethod <IComponentContext>(cc => cc.ResolveComponent(default(IComponentRegistration), default(Parameter[]))),
                           resolveParams));
            },
                                         delegateType,
                                         GetParameterMapping(delegateType, parameterMapping));
        }
Ejemplo n.º 2
0
        public void ValidateNonAspect_Test()
        {
            var validator = AspectValidatorFactory.GetAspectValidator(new AspectConfigure());

            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <ClassNonAspectValidatorModel> >(m => m.Validate())));
            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <MethodNonAspectValidatorModel> >(m => m.Validate())));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a factory generator.
        /// </summary>
        /// <param name="service">The service that will be activated in
        /// order to create the products of the factory.</param>
        /// <param name="delegateType">The delegate to provide as a factory.</param>
        /// <param name="parameterMapping">The parameter mapping mode to use.</param>
        public FactoryGenerator(Type delegateType, Service service, ParameterMapping parameterMapping)
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }
            Enforce.ArgumentTypeIsFunction(delegateType);

            _generator = CreateGenerator((activatorContextParam, resolveParameterArray) =>
            {
                // c, service, [new Parameter(name, (object)dps)]*
                var resolveParams = new[] {
                    activatorContextParam,
                    Expression.Constant(service, typeof(Service)),
                    Expression.NewArrayInit(typeof(Parameter), resolveParameterArray)
                };

                // c.Resolve(...)
                return(Expression.Call(
                           ReflectionExtensions.GetMethod <IComponentContext>(cc => cc.ResolveService(default(Service), default(Parameter[]))),
                           resolveParams));
            },
                                         delegateType,
                                         GetParameterMapping(delegateType, parameterMapping));
        }
Ejemplo n.º 4
0
        public void ValidateInterceptor_Test()
        {
            var validator = AspectValidatorFactory.GetAspectValidator(new AspectConfigure());

            Assert.True(validator.Validate(ReflectionExtensions.GetMethod <Action <ClassWithInterceptorValidatorModel> >(m => m.Validate())));
            Assert.True(validator.Validate(ReflectionExtensions.GetMethod <Action <MethodWithInterceptorValidatorModel> >(m => m.Validate())));
        }
Ejemplo n.º 5
0
        public void ValidateDeclaringMethod_Test()
        {
            var validator = AspectValidatorFactory.GetAspectValidator(new AspectConfigure());

            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <FinalMethodValidatorModel> >(m => m.Validate())));
            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <NonPublicMethodValidatorModel> >(m => m.Validate())));
            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action>(() => StaticMethodValidatorModel.Validate())));
        }
Ejemplo n.º 6
0
        public void ValidateDeclaringType_Test()
        {
            var validator = AspectValidatorFactory.GetAspectValidator(new AspectConfigure());

            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <NotPublicValidatorModel> >(m => m.Validate())));
            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <VauleTypeValidatorModel> >(m => m.Validate())));
            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <SealedValidatorModel> >(m => m.Validate())));
        }
Ejemplo n.º 7
0
        public void Configure_ValidateInterceptor_Test()
        {
            var configure = new AspectConfigure();

            configure.GetConfigureOption <IInterceptor>().Add(m => new IncrementAttribute());
            var validator = AspectValidatorFactory.GetAspectValidator(configure);

            Assert.True(validator.Validate(ReflectionExtensions.GetMethod <Action <ConfigureInterceptorValidatorModel> >(m => m.Validate())));
        }
Ejemplo n.º 8
0
        public void ValidateIgnoredList_Test()
        {
            var configure = new AspectConfigure();

            configure.GetConfigureOption <bool>().Add(m => m.DeclaringType.Name.Matches("IgnoredList*"));
            var validator = AspectValidatorFactory.GetAspectValidator(configure);

            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <IgnoredListValidatorModel> >(m => m.Validate())));
            Assert.False(validator.Validate(ReflectionExtensions.GetMethod <Action <object> >(m => m.ToString())));
        }
Ejemplo n.º 9
0
        public void With_Type_Interceptor_Test()
        {
            var Configure = new AspectConfigure();

            var matcher = new InterceptorMatcher(Configure);
            var method  = ReflectionExtensions.GetMethod <Action <WithInterceptorMatcherModel> >(m => m.WithOutInterceptor());

            var interceptors = matcher.Match(method, typeof(WithInterceptorMatcherModel).GetTypeInfo());

            Assert.NotEmpty(interceptors);
            Assert.Single(interceptors);
        }
Ejemplo n.º 10
0
        public void WithOutInterceptor_Test()
        {
            var Configure = new AspectConfigure();

            var matcher = new InterceptorMatcher(Configure);

            var method = ReflectionExtensions.GetMethod <Action <InterceptorMatcherModel> >(m => m.WithOutInterceptor());

            var interceptors = matcher.Match(method, method.DeclaringType.GetTypeInfo());

            Assert.Empty(interceptors);
        }
Ejemplo n.º 11
0
        protected override void GeneratingMethodBody(ILGenerator ilGenerator)
        {
            ilGenerator.EmitThis();
            ilGenerator.Emit(OpCodes.Ldfld, _serviceProviderFieldBuilder);
            var getAspectActivator = ReflectionExtensions.GetMethod <Func <IServiceProvider, IAspectActivator> >(provider => provider.GetAspectActivator());

            ilGenerator.Emit(OpCodes.Call, getAspectActivator);  //var aspectActivator = this.serviceProvider.GetService<IAspectActivator>();
            GeneratingInitializeMetaData(ilGenerator);

            var aspectActivatorContextType = new AspectActivatorContextGenerator().CreateType();
            var ctors = aspectActivatorContextType.GetConstructors();

            ilGenerator.Emit(OpCodes.Newobj, ctors[0]);
            GeneratingReturnVaule(ilGenerator);
            ilGenerator.Emit(OpCodes.Ret);
        }
Ejemplo n.º 12
0
        public void With_Configure_Interceptor_Test()
        {
            var Configure = new AspectConfigure();

            var ConfigureInterceptor = new InjectedInterceptor();

            Configure.GetConfigureOption <IInterceptor>().Add(m => ConfigureInterceptor);

            var matcher = new InterceptorMatcher(Configure);
            var method  = ReflectionExtensions.GetMethod <Action <InterceptorMatcherModel> >(m => m.ConfigureInterceptor());

            var interceptors = matcher.Match(method, method.DeclaringType.GetTypeInfo());

            Assert.NotEmpty(interceptors);

            Assert.Single(interceptors, ConfigureInterceptor);
        }
Ejemplo n.º 13
0
        public void InvokeA_Test()
        {
            var Configure       = new AspectConfigure();
            var serviceProvider = new InstanceServiceProvider(null);
            var activator       = new AspectActivator(serviceProvider,
                                                      new AspectBuilderProvider(new InterceptorSelector(new InterceptorMatcher(Configure), new InterceptorInjectorProvider(serviceProvider, new PropertyInjectorSelector()))));

            var input = 0;

            var activatorContext = Substitute.For <AspectActivatorContext>();

            activatorContext.Parameters.Returns(new object[] { input });
            activatorContext.ServiceType.Returns(typeof(ITargetService));
            activatorContext.ServiceMethod.Returns(ReflectionExtensions.GetMethod <Func <ITargetService, int, int> >((m, v) => m.Add(v)));
            activatorContext.TargetMethod.Returns(ReflectionExtensions.GetMethod <Func <TargetService, int, int> >((m, v) => m.Add(v)));
            activatorContext.ProxyMethod.Returns(ReflectionExtensions.GetMethod <Func <ProxyService, int, int> >((m, v) => m.Add(v)));
            activatorContext.TargetInstance.Returns(new TargetService());
            activatorContext.ProxyInstance.Returns(new ProxyService());

            var result = activator.Invoke <int>(activatorContext);

            Assert.Equal(result, input + 1);
        }
Ejemplo n.º 14
0
        public FactoryGenerator(Type delegateType, Service service, ParameterMapping parameterMapping)
        {
            Func <Expression, Expression[], Expression> makeResolveCall = null;

            if (service == null)
            {
                throw new ArgumentNullException("service");
            }
            Enforce.ArgumentTypeIsFunction(delegateType);
            if (makeResolveCall == null)
            {
                makeResolveCall = (activatorContextParam, resolveParameterArray) => Expression.Call(ReflectionExtensions.GetMethod <IComponentContext>((Expression <Action <IComponentContext> >)(cc => cc.ResolveService(null, ((Parameter[])null)))), new Expression[] { activatorContextParam, Expression.Constant(service, typeof(Service)), Expression.NewArrayInit(typeof(Parameter), resolveParameterArray) });
            }
            this._generator = CreateGenerator(makeResolveCall, delegateType, GetParameterMapping(delegateType, parameterMapping));
        }
Ejemplo n.º 15
0
        public FactoryGenerator(Type delegateType, IComponentRegistration productRegistration, ParameterMapping parameterMapping)
        {
            Func <Expression, Expression[], Expression> makeResolveCall = null;

            if (productRegistration == null)
            {
                throw new ArgumentNullException("productRegistration");
            }
            Enforce.ArgumentTypeIsFunction(delegateType);
            if (makeResolveCall == null)
            {
                makeResolveCall = (activatorContextParam, resolveParameterArray) => Expression.Call(activatorContextParam, ReflectionExtensions.GetMethod <IComponentContext>((Expression <Action <IComponentContext> >)(cc => cc.ResolveComponent(null, null))), new Expression[] { Expression.Constant(productRegistration, typeof(IComponentRegistration)), Expression.NewArrayInit(typeof(Parameter), resolveParameterArray) });
            }
            this._generator = CreateGenerator(makeResolveCall, delegateType, GetParameterMapping(delegateType, parameterMapping));
        }