Skip to content

Commit dfc3afa

Browse files
committed
Fixed #72
1 parent 9cf7960 commit dfc3afa

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

package.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<Version>5.7.3</Version>
4+
<Version>5.7.4</Version>
55
<PackageReleaseNotes>This package is distributed as .NET Standard 1.0, .NET 4.0, 4.5, 4.6, 4.7 package.</PackageReleaseNotes>
66
</PropertyGroup>
77

src/Policy/BuildPlanCreator/GenericLazyBuildPlanCreatorPolicy.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2-
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53
using System.Reflection;
64
using Unity.Builder;
@@ -16,6 +14,7 @@ public class GenericLazyBuildPlanCreatorPolicy : IBuildPlanCreatorPolicy
1614
{
1715
private static readonly MethodInfo BuildResolveLazyMethod;
1816
private static readonly MethodInfo BuildResolveAllLazyMethod;
17+
private static readonly MethodInfo BuildResolveLazyEnumerableMethod;
1918

2019
static GenericLazyBuildPlanCreatorPolicy()
2120
{
@@ -26,6 +25,9 @@ static GenericLazyBuildPlanCreatorPolicy()
2625

2726
BuildResolveAllLazyMethod =
2827
info.GetDeclaredMethod(nameof(BuildResolveAllLazy));
28+
29+
BuildResolveLazyEnumerableMethod =
30+
info.GetDeclaredMethod(nameof(BuildResolveLazyEnumerable));
2931
}
3032

3133
/// <summary>
@@ -47,9 +49,10 @@ private static DynamicBuildPlanMethod CreateBuildPlanMethod(Type lazyType)
4749

4850
MethodInfo lazyMethod;
4951

50-
if (itemType.GetTypeInfo().IsGenericType && itemType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
52+
var info = itemType.GetTypeInfo();
53+
if (info.IsGenericType && itemType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
5154
{
52-
lazyMethod = BuildResolveAllLazyMethod.MakeGenericMethod(itemType.GetTypeInfo().GenericTypeArguments[0]);
55+
lazyMethod = BuildResolveLazyEnumerableMethod.MakeGenericMethod(itemType.GetTypeInfo().GenericTypeArguments[0]);
5356
}
5457
else
5558
{
@@ -66,18 +69,26 @@ private static void BuildResolveLazy<T>(IBuilderContext context)
6669
context.Existing = new Lazy<T>(() => context.Container.Resolve<T>(context.BuildKey.Name));
6770
}
6871

69-
// match the behavior of DynamicMethodConstructorStrategy
72+
context.SetPerBuildSingleton();
73+
}
74+
75+
private static void BuildResolveLazyEnumerable<T>(IBuilderContext context)
76+
{
77+
if (context.Existing == null)
78+
{
79+
context.Existing = new Lazy<IEnumerable<T>>(() => context.Container.Resolve<IEnumerable<T>>());
80+
}
81+
7082
context.SetPerBuildSingleton();
7183
}
7284

7385
private static void BuildResolveAllLazy<T>(IBuilderContext context)
7486
{
7587
if (context.Existing == null)
7688
{
77-
context.Existing = new Lazy<IEnumerable<T>>(() => context.Container.ResolveAll<T>());
89+
context.Existing = new Lazy<T[]>(() => (T[])context.Container.ResolveAll<T>());
7890
}
7991

80-
// match the behavior of DynamicMethodConstructorStrategy
8192
context.SetPerBuildSingleton();
8293
}
8394
}

src/Strategies/ArrayResolveStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public override void PreBuildUp(IBuilderContext context)
4242
var type = context.OriginalBuildKey.Type.GetElementType();
4343
var info = type.GetTypeInfo();
4444
var buildMethod = info.IsGenericType && typeof(Lazy<>) == info.GetGenericTypeDefinition()
45-
? _resolveLazyMethod.MakeGenericMethod().CreateDelegate(typeof(DynamicBuildPlanMethod))
46-
: _resolveMethod.MakeGenericMethod().CreateDelegate(typeof(DynamicBuildPlanMethod));
45+
? _resolveLazyMethod.MakeGenericMethod(type).CreateDelegate(typeof(DynamicBuildPlanMethod))
46+
: _resolveMethod.MakeGenericMethod(type).CreateDelegate(typeof(DynamicBuildPlanMethod));
4747

4848
plan = new DynamicMethodBuildPlan((DynamicBuildPlanMethod)buildMethod);
4949
context.Registration.Set(typeof(IBuildPlanPolicy), plan);

src/Strategies/BuildKeyMappingStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public override bool RequiredToBuildType(IUnityContainer container, INamedType n
102102

103103
private bool AnalysStaticRegistration(IUnityContainer container, ContainerRegistration registration, params InjectionMember[] injectionMembers)
104104
{
105-
// Validate imput
105+
// Validate input
106106
if (null == registration.MappedToType || registration.RegisteredType == registration.MappedToType) return false;
107107

108108
// Require Re-Resolve if no injectors specified

src/Strategies/BuildPlanStrategy.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class BuildPlanStrategy : BuilderStrategy
2525
public override void PreBuildUp(IBuilderContext context)
2626
{
2727
var plan = context.Registration.Get<IBuildPlanPolicy>() ??
28-
(IBuildPlanPolicy)GetExtended(context.Policies, typeof(IBuildPlanPolicy),
28+
(IBuildPlanPolicy)GetGeneric(context.Policies, typeof(IBuildPlanPolicy),
2929
context.OriginalBuildKey,
3030
context.OriginalBuildKey.Type);
3131

@@ -52,14 +52,12 @@ public override void PreBuildUp(IBuilderContext context)
5252

5353
public static TPolicyInterface GetPolicy<TPolicyInterface>(IPolicyList list, INamedType buildKey)
5454
{
55-
return (TPolicyInterface)(GetExtended(list, typeof(TPolicyInterface), buildKey, buildKey.Type) ??
55+
return (TPolicyInterface)(GetGeneric(list, typeof(TPolicyInterface), buildKey, buildKey.Type) ??
5656
list.Get(null, null, typeof(TPolicyInterface), out _)); // Nothing! Get Default
5757
}
5858

59-
private static IBuilderPolicy GetExtended(IPolicyList list, Type policyInterface, INamedType buildKey, Type buildType)
59+
private static IBuilderPolicy GetGeneric(IPolicyList list, Type policyInterface, INamedType buildKey, Type buildType)
6060
{
61-
if (null == buildType) return null;
62-
6361
// Check if generic
6462
if (buildType.GetTypeInfo().IsGenericType)
6563
{
@@ -68,8 +66,7 @@ private static IBuilderPolicy GetExtended(IPolicyList list, Type policyInterface
6866
list.Get(newType, string.Empty, policyInterface, out _);
6967
}
7068

71-
// Check default for type
72-
return list.Get(buildType, string.Empty, policyInterface, out _);
69+
return null;
7370
}
7471

7572
private static IBuildPlanCreatorPolicy CheckIfOpenGeneric(IPolicySet namedType)

0 commit comments

Comments
 (0)