Esempio n. 1
0
 public DelegateParameterInfo(ParameterInfo parameterInfo)
 {
     ParameterInfo = parameterInfo;
     UniqueId      = UniqueStringId.Generate();
 }
Esempio n. 2
0
        /// <inheritdoc />
        protected override IActivationExpressionResult CreateExpression(IInjectionScope scope, IActivationExpressionRequest request, ICompiledLifestyle lifestyle)
        {
            if (_proxyType == null)
            {
                lock (_proxyTypeLock)
                {
                    if (_proxyType == null)
                    {
                        var builder = new DynamicTypeBuilder();

                        _proxyType = builder.CreateType(ActivationType, out _delegateInfo);
                    }
                }
            }

            request.RequireExportScope();
            request.RequireDisposalScope();
            request.RequireInjectionContext();

            var parameters = new List <Expression>
            {
                request.ScopeParameter,
                request.DisposalScopeExpression,
                request.InjectionContextParameter
            };

            var uniqueId = UniqueStringId.Generate();

            foreach (var delegateInfo in _delegateInfo)
            {
                var locateType = delegateInfo.Method.ReturnType;

                var newRequest = request.NewRequest(locateType, this, ActivationType, RequestType.Other, null, true);

                newRequest.AddKnownValueExpression(
                    CreateKnownValueExpression(newRequest, ActivationType, uniqueId));

                if (delegateInfo.Method.Name.StartsWith("Get"))
                {
                    newRequest.SetLocateKey(delegateInfo.Method.Name.Substring("Get".Length));
                }

                if (delegateInfo.ParameterInfos != null)
                {
                    foreach (var parameter in delegateInfo.ParameterInfos)
                    {
                        newRequest.AddKnownValueExpression(
                            CreateKnownValueExpression(newRequest, parameter.ParameterInfo.ParameterType, parameter.UniqueId, parameter.ParameterInfo.Name, parameter.ParameterInfo.Position));
                    }
                }

                var result = request.Services.ExpressionBuilder.GetActivationExpression(request.RequestingScope, newRequest);

                var compiledDelegate = request.Services.Compiler.CompileDelegate(request.RequestingScope, result);

                parameters.Add(Expression.Constant(compiledDelegate));
            }

            var constructor = _proxyType.GetTypeInfo().DeclaredConstructors.First();

            request.RequireInjectionContext();

            var newStatement = Expression.New(constructor, parameters);

            var setMethod = typeof(IExtraDataContainer).GetRuntimeMethod(nameof(IExtraDataContainer.SetExtraData),
                                                                         new[] { typeof(object), typeof(object), typeof(bool) });

            var invokeStatement = Expression.Call(request.InjectionContextParameter, setMethod,
                                                  Expression.Constant(uniqueId), newStatement, Expression.Constant(true));

            var castStatement = Expression.Convert(invokeStatement, ActivationType);

            return(request.Services.Compiler.CreateNewResult(request, castStatement));
        }