Skip to content

Commit 5c0c388

Browse files
pksorensenENikS
authored andcommitted
added default paramter support (#13)
unitycontainer/container#60 (comment)
1 parent e4e886e commit 5c0c388

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/Policy/ConstructorSelectorPolicy.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyLis
2727
{
2828
ConstructorInfo ctor = FindDependencyConstructor<DependencyAttribute>(context);
2929
if (ctor != null)
30-
return CreateSelectedConstructor(ctor);
30+
return CreateSelectedConstructor(ctor,context);
3131
return _dependency.SelectConstructor(context, resolverPolicyDestination);
3232
}
3333

@@ -61,12 +61,12 @@ private static ConstructorInfo FindSingleConstructor(IEnumerable<ConstructorInfo
6161
return null;
6262
}
6363

64-
private SelectedConstructor CreateSelectedConstructor(ConstructorInfo ctor)
64+
private SelectedConstructor CreateSelectedConstructor(ConstructorInfo ctor, IBuilderContext context)
6565
{
6666
var result = new SelectedConstructor(ctor);
6767
foreach (ParameterInfo param in ctor.GetParameters())
6868
{
69-
result.AddParameterResolver(ResolveParameter(param));
69+
result.AddParameterResolver(param.HasDefaultValue ? context.Container.CanResolve(param.ParameterType)? ResolveParameter(param): new LiteralValueDependencyResolverPolicy(null) : ResolveParameter(param));
7070
}
7171
return result;
7272
}
@@ -141,7 +141,7 @@ private ConstructorInfo Other(ConstructorInfo[] constructors, IBuilderContext co
141141

142142
private bool CanBuildUp(ParameterInfo[] parameters, IBuilderContext context)
143143
{
144-
return parameters.All(p => context.Container.CanResolve(p.ParameterType));
144+
return parameters.All(p => context.Container.CanResolve(p.ParameterType) || p.HasDefaultValue);
145145
}
146146

147147
/// <summary>

0 commit comments

Comments
 (0)