コード例 #1
0
        private Expression BuildParameter(IDependency parameterDependency, Type parameterType, Expression argument,
            IDependencyScope scope)
        {
            if (parameterDependency == null) return Expression.Default(parameterType);

            switch (parameterDependency.Lifetime)
            {
                case DependencyLifetime.Scoped:
                case DependencyLifetime.Transient:
                    var dependencyConstant = Expression.Constant(parameterDependency);

                    // ReSharper disable AssignNullToNotNullAttribute
                    var getInstanceMethod = parameterDependency.GetType().GetMethod(nameof(IDependency.GetInstance));
                    var parameterInstanceCall = Expression.Call(
                        dependencyConstant, getInstanceMethod,
                        Expression.Constant(parameterType), argument);
                    // ReSharper restore AssignNullToNotNullAttribute

                    return Expression.Convert(parameterInstanceCall, parameterType);

                case DependencyLifetime.Singleton:
                    var parameterConstant = parameterDependency.GetInstance(parameterType, scope);
                    return Expression.Constant(parameterConstant);
            }

            throw Error.InvalidDependencyLifetime();
        }
コード例 #2
0
ファイル: CompiledResolver.cs プロジェクト: teoadal/velo
        private static Expression BuildParameter(
            IDependency parameterDependency,
            Type parameterType,
            IServiceProvider services,
            Expression servicesArgument)
        {
            if (parameterDependency.Lifetime == DependencyLifetime.Singleton)
            {
                var instance = parameterDependency.GetInstance(parameterType, services);
                return(Expression.Constant(instance));
            }

            var dependency = Expression.Constant(parameterDependency);

            var parameterCall = Expression.Call(dependency, GetInstanceMethod,
                                                Expression.Constant(parameterType),
                                                servicesArgument);

            return(Expression.Convert(parameterCall, parameterType));
        }