Added ComponentModelConverter to utilize System.ComponentModel TypeConverters Very...
[castle.git] / InversionOfControl / Castle.MicroKernel / SubSystems / Conversion / DefaultConversionManager.cs
blobc696b504848a1c0daee6ac71bc883d5eb7cfdc15
1 // Copyright 2004-2007 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 System.Collections;
19 using System.Threading;
20 using Castle.Core;
21 using Castle.Core.Configuration;
23 /// <summary>
24 /// Composition of all available conversion managers
25 /// </summary>
26 [Serializable]
27 public class DefaultConversionManager : AbstractSubSystem, IConversionManager, ITypeConverterContext
29 private static LocalDataStoreSlot slot = Thread.AllocateDataSlot();
30 private IList converters;
31 private IList standAloneConverters;
33 public DefaultConversionManager()
35 converters = new ArrayList();
36 standAloneConverters = new ArrayList();
38 InitDefaultConverters();
41 protected virtual void InitDefaultConverters()
43 Add(new PrimitiveConverter());
44 Add(new TimeSpanConverter());
45 Add(new TypeNameConverter());
46 Add(new EnumConverter());
47 Add(new ListConverter());
48 Add(new DictionaryConverter());
49 Add(new GenericDictionaryConverter());
50 Add(new GenericListConverter());
51 Add(new ArrayConverter());
52 Add(new ComponentConverter());
53 Add(new AttributeAwareConverter());
54 Add(new ComponentModelConverter());
57 #region IConversionManager Members
59 public void Add(ITypeConverter converter)
61 converter.Context = this;
63 converters.Add(converter);
65 if (!(converter is IKernelDependentConverter))
67 standAloneConverters.Add(converter);
71 public bool IsSupportedAndPrimitiveType(Type type)
73 foreach(ITypeConverter converter in standAloneConverters)
75 if (converter.CanHandleType(type)) return true;
78 return false;
81 #endregion
83 #region ITypeConverter Members
85 public ITypeConverterContext Context
87 get { return this; }
88 set { throw new NotImplementedException(); }
91 public bool CanHandleType(Type type)
93 foreach(ITypeConverter converter in converters)
95 if (converter.CanHandleType(type)) return true;
98 return false;
101 public bool CanHandleType(Type type, IConfiguration configuration)
103 foreach(ITypeConverter converter in converters)
105 if (converter.CanHandleType(type, configuration)) return true;
108 return false;
111 public object PerformConversion(String value, Type targetType)
113 foreach(ITypeConverter converter in converters)
115 if (converter.CanHandleType(targetType))
117 return converter.PerformConversion(value, targetType);
121 String message = String.Format("No converter registered to handle the type {0}",
122 targetType.FullName);
124 throw new ConverterException(message);
127 public object PerformConversion(IConfiguration configuration, Type targetType)
129 foreach(ITypeConverter converter in converters)
131 if (converter.CanHandleType(targetType, configuration))
133 return converter.PerformConversion(configuration, targetType);
137 String message = String.Format("No converter registered to handle the type {0}",
138 targetType.FullName);
140 throw new ConverterException(message);
143 #endregion
145 #region ITypeConverterContext Members
147 IKernel ITypeConverterContext.Kernel
149 get { return base.Kernel; }
152 public void PushModel(ComponentModel model)
154 CurrentStack.Push(model);
157 public void PopModel()
159 CurrentStack.Pop();
162 public ComponentModel CurrentModel
166 if (CurrentStack.Count == 0) return null;
167 else return (ComponentModel) CurrentStack.Peek();
171 public ITypeConverter Composition
173 get { return this; }
176 #endregion
178 private Stack CurrentStack
182 Stack stack = (Stack) Thread.GetData(slot);
184 if (stack == null)
186 stack = new Stack();
187 Thread.SetData(slot, stack);
190 return stack;