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(); }
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)); }