Skip to content

Commit a3133bc

Browse files
tothdavidscott-xu
authored andcommitted
Handle generic Lazy parameters correctly in LazyConstructorScorer (#42)
1 parent 743b007 commit a3133bc

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//-------------------------------------------------------------------------------
2+
// <copyright file="ClassWithGenericLazy.cs" company="Ninject Project Contributors">
3+
// Copyright (c) 2009-2018 Ninject Project Contributors
4+
//
5+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
6+
// You may not use this file except in compliance with one of the Licenses.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
// or
11+
// http://www.microsoft.com/opensource/licenses.mspx
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
// </copyright>
19+
//-------------------------------------------------------------------------------
20+
21+
#if !NET_35 && !SILVERLIGHT_30 && !SILVERLIGHT_20 && !WINDOWS_PHONE && !NETCF_35
22+
namespace Ninject.Extensions.Factory.Fakes
23+
{
24+
using System;
25+
26+
public class ClassWithGenericLazy
27+
{
28+
private readonly Lazy<Lazy<IWeapon>> lazyWeapons;
29+
30+
public ClassWithGenericLazy(Lazy<Lazy<IWeapon>> lazyWeapons)
31+
{
32+
this.lazyWeapons = lazyWeapons;
33+
}
34+
35+
public IWeapon GetWeapon()
36+
{
37+
return this.lazyWeapons.Value.Value;
38+
}
39+
}
40+
}
41+
#endif

src/Ninject.Extensions.Factory.Test/FuncTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ public void LazyInjection()
197197
var weapon1 = service.GetWeapon();
198198
var weapon2 = service.GetWeapon();
199199

200+
weapon1.Should().BeOfType<Sword>();
201+
weapon2.Should().BeOfType<Sword>();
202+
weapon1.Should().BeSameAs(weapon2);
203+
}
204+
205+
[Fact]
206+
public void GenericLazyInjection()
207+
{
208+
this.kernel.Bind<IWeapon>().To<Sword>();
209+
210+
var service = this.kernel.Get<ClassWithGenericLazy>();
211+
var weapon1 = service.GetWeapon();
212+
var weapon2 = service.GetWeapon();
213+
200214
weapon1.Should().BeOfType<Sword>();
201215
weapon2.Should().BeOfType<Sword>();
202216
weapon1.Should().BeSameAs(weapon2);

src/Ninject.Extensions.Factory/Func/LazyConstructorScorer.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,30 @@ public override int Score(IContext context, ConstructorInjectionDirective direct
4646
if (directive.Constructor.GetParameters().Length == 1 &&
4747
directive.Constructor.GetParameters()[0].ParameterType.IsGenericType)
4848
{
49-
return 1;
50-
}
51-
else
52-
{
53-
return 0;
49+
var parameterGenericType = directive.Constructor.GetParameters()[0].ParameterType.GetGenericTypeDefinition();
50+
if (parameterGenericType == typeof(Func<>)
51+
|| parameterGenericType == typeof(Func<,>)
52+
|| parameterGenericType == typeof(Func<,,>)
53+
|| parameterGenericType == typeof(Func<,,,>)
54+
|| parameterGenericType == typeof(Func<,,,,>)
55+
|| parameterGenericType == typeof(Func<,,,,,>)
56+
|| parameterGenericType == typeof(Func<,,,,,,>)
57+
|| parameterGenericType == typeof(Func<,,,,,,,>)
58+
|| parameterGenericType == typeof(Func<,,,,,,,,>)
59+
|| parameterGenericType == typeof(Func<,,,,,,,,,>)
60+
|| parameterGenericType == typeof(Func<,,,,,,,,,,>)
61+
|| parameterGenericType == typeof(Func<,,,,,,,,,,,>)
62+
|| parameterGenericType == typeof(Func<,,,,,,,,,,,,>)
63+
|| parameterGenericType == typeof(Func<,,,,,,,,,,,,,>)
64+
|| parameterGenericType == typeof(Func<,,,,,,,,,,,,,,>)
65+
|| parameterGenericType == typeof(Func<,,,,,,,,,,,,,,,>)
66+
|| parameterGenericType == typeof(Func<,,,,,,,,,,,,,,,,>))
67+
{
68+
return 1;
69+
}
5470
}
71+
72+
return 0;
5573
}
5674
else
5775
{

0 commit comments

Comments
 (0)