Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / InversionOfControl / Castle.MicroKernel / SubSystems / Conversion / Converters / PrimitiveConverter.cs
blob0caa254b775565f4a2d6e8794f6efbaa0a1316fc
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using Castle.Core.Configuration;
20 /// <summary>
21 /// Implements all standard conversions.
22 /// </summary>
23 [Serializable]
24 public class PrimitiveConverter : AbstractTypeConverter
26 private Type[] types;
28 public PrimitiveConverter()
30 types = new Type[]
32 typeof(Char),
33 typeof(DateTime),
34 typeof(Decimal),
35 typeof(Boolean),
36 typeof(Int16),
37 typeof(Int32),
38 typeof(Int64),
39 typeof(UInt16),
40 typeof(UInt32),
41 typeof(UInt64),
42 typeof(Byte),
43 typeof(SByte),
44 typeof(Single),
45 typeof(Double),
46 typeof(String)
50 public override bool CanHandleType(Type type)
52 return Array.IndexOf(types, type) != -1;
55 public override object PerformConversion(String value, Type targetType)
57 if (targetType == typeof(String)) return value;
59 try
61 return Convert.ChangeType(value, targetType);
63 catch(Exception ex)
65 String message = String.Format(
66 "Could not convert from '{0}' to {1}",
67 value, targetType.FullName);
69 throw new ConverterException(message, ex);
73 public override object PerformConversion(IConfiguration configuration, Type targetType)
75 return PerformConversion(configuration.Value, targetType);