1 // Copyright 2004-2008 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
.SubSystems
.Conversion
19 using Castle
.MicroKernel
.Util
;
21 using Castle
.Core
.Configuration
;
24 public class ComponentConverter
: AbstractTypeConverter
, IKernelDependentConverter
26 public override bool CanHandleType(Type type
, IConfiguration configuration
)
28 if (configuration
.Value
!= null)
30 return ReferenceExpressionUtil
.IsReference(configuration
.Value
.Trim());
34 return CanHandleType(type
);
38 public override bool CanHandleType(Type type
)
40 if (Context
.Kernel
== null) return false;
42 return Context
.Kernel
.HasComponent(type
);
45 public override object PerformConversion(String
value, Type targetType
)
47 if (!ReferenceExpressionUtil
.IsReference(value))
49 String message
= String
.Format("Could not convert expression {0} to type {1}. Expecting a reference override like ${some key}", value, targetType
.FullName
);
50 throw new ConverterException(message
);
53 String key
= ReferenceExpressionUtil
.ExtractComponentKey(value);
55 DependencyModel dependency
= new DependencyModel(DependencyType
.ServiceOverride
, key
, targetType
, false);
57 return Context
.Kernel
.Resolver
.Resolve(CreationContext
.Empty
, null, Context
.CurrentModel
, dependency
);
60 public override object PerformConversion(IConfiguration configuration
, Type targetType
)
62 return PerformConversion(configuration
.Value
, targetType
);