1+ // -------------------------------------------------------------------------------------------------
2+ // <copyright file="LazyConstructorScorer.cs" company="Ninject Project Contributors">
3+ // Copyright (c) 2009-2017 Ninject Project Contributors
4+ // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
5+ // </copyright>
6+ // -------------------------------------------------------------------------------------------------
7+
8+ namespace Ninject . Extensions . Factory
9+ {
10+ using System ;
11+ using Ninject . Activation ;
12+ using Ninject . Planning . Directives ;
13+ using Ninject . Selection . Heuristics ;
14+
15+ /// <summary>
16+ /// Scores constructors by either looking for the existence of an injection marker
17+ /// attribute, or by counting the number of parameters.
18+ /// </summary>
19+ public class LazyConstructorScorer : StandardConstructorScorer
20+ {
21+ /// <summary>
22+ /// Gets the score for the specified constructor.
23+ /// </summary>
24+ /// <param name="context">The injection context.</param>
25+ /// <param name="directive">The constructor.</param>
26+ /// <returns>The constructor's score.</returns>
27+ public override int Score ( IContext context , ConstructorInjectionDirective directive )
28+ {
29+ if ( context . Request . Service . IsGenericType &&
30+ context . Request . Service . GetGenericTypeDefinition ( ) == typeof ( Lazy < > ) )
31+ {
32+ if ( directive . Constructor . GetParameters ( ) . Length == 1 &&
33+ directive . Constructor . GetParameters ( ) [ 0 ] . ParameterType . IsGenericType )
34+ {
35+ return 1 ;
36+ }
37+ else
38+ {
39+ return 0 ;
40+ }
41+ }
42+ else
43+ {
44+ return base . Score ( context , directive ) ;
45+ }
46+ }
47+ }
48+ }
0 commit comments