Flavien/quickwire

InjectConfiguration should ignore null value

li-zheng-hao opened this issue · 0 comments

[RegisterService]
public class Config
{
    [InjectConfiguration("test:not_exist")]
    public string Test { get; set; } = "default";
}

When Test field is not set in appsettings.json file, this value should be the default value instead of null ,

public Func<IServiceProvider, object> GetFactory(Type type)
{
    if (type.ContainsGenericParameters)
        throw new ArgumentException($"The service type '{type.FullName}' must not be generic.");

    ConstructorInfo constructorInfo = GetConstructor(type);
    ParameterInfo[] parameters = constructorInfo.GetParameters();
    IDependencyResolver?[] dependencyResolvers = GetParametersDependencyResolvers(parameters);
    DelegateCompiler.Constructor constructor = DelegateCompiler.CreateConstructor(constructorInfo);
    List<SetterInfo> setters = GetSetters(type);

    return delegate (IServiceProvider serviceProvider)
    {
        object?[] arguments = new object[parameters.Length];

        for (int i = 0; i < parameters.Length; i++)
            arguments[i] = Resolve(serviceProvider, parameters[i].ParameterType, dependencyResolvers[i]);

        object result = constructor(arguments);

        foreach (SetterInfo setter in setters)
        {
            // need check null here
            object? resolvedDependency = Resolve(serviceProvider, setter.ServiceType, setter.DependencyResolver);
            setter.Setter(result, resolvedDependency);
        }

        return result;
    };
}