1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MicroKernel
.ModelBuilder
.Inspectors
18 using System
.Reflection
;
20 using Castle
.MicroKernel
.SubSystems
.Conversion
;
23 /// This implementation of <see cref="IContributeComponentModelConstruction"/>
24 /// collects all available constructors and populates them in the model
25 /// as candidates. The Kernel will pick up one of the candidates
26 /// according to a heuristic.
29 public class ConstructorDependenciesModelInspector
: IContributeComponentModelConstruction
32 private IConversionManager converter
;
34 public ConstructorDependenciesModelInspector()
38 public virtual void ProcessModel(IKernel kernel
, ComponentModel model
)
40 if (converter
== null)
42 converter
= (IConversionManager
)
43 kernel
.GetSubSystem(SubSystemConstants
.ConversionManagerKey
);
46 Type targetType
= model
.Implementation
;
48 ConstructorInfo
[] constructors
=
49 targetType
.GetConstructors(BindingFlags
.Public
| BindingFlags
.Instance
);
51 foreach(ConstructorInfo constructor
in constructors
)
53 // We register each public constructor
54 // and let the ComponentFactory select an
55 // eligible amongst the candidates later
57 model
.Constructors
.Add(CreateConstructorCandidate(constructor
));
61 protected virtual ConstructorCandidate
CreateConstructorCandidate(ConstructorInfo constructor
)
63 ParameterInfo
[] parameters
= constructor
.GetParameters();
65 DependencyModel
[] dependencies
= new DependencyModel
[parameters
.Length
];
67 for(int i
= 0; i
< parameters
.Length
; i
++)
69 ParameterInfo parameter
= parameters
[i
];
71 Type paramType
= parameter
.ParameterType
;
73 // This approach is somewhat problematic. We should use
74 // another strategy to differentiate types and classify dependencies
75 if (converter
.IsSupportedAndPrimitiveType(paramType
))
77 dependencies
[i
] = new DependencyModel(
78 DependencyType
.Parameter
, parameter
.Name
, paramType
, false);
82 dependencies
[i
] = new DependencyModel(
83 DependencyType
.Service
, parameter
.Name
, paramType
, false);
87 return new ConstructorCandidate(constructor
, dependencies
);