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
.SubSystems
.Conversion
18 using System
.Collections
;
19 using System
.Threading
;
21 using Castle
.Core
.Configuration
;
24 /// Composition of all available conversion managers
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;
83 #region ITypeConverter Members
85 public ITypeConverterContext Context
88 set { throw new NotImplementedException(); }
91 public bool CanHandleType(Type type
)
93 foreach(ITypeConverter converter
in converters
)
95 if (converter
.CanHandleType(type
)) return true;
101 public bool CanHandleType(Type type
, IConfiguration configuration
)
103 foreach(ITypeConverter converter
in converters
)
105 if (converter
.CanHandleType(type
, configuration
)) return true;
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
);
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()
162 public ComponentModel CurrentModel
166 if (CurrentStack
.Count
== 0) return null;
167 else return (ComponentModel
) CurrentStack
.Peek();
171 public ITypeConverter Composition
178 private Stack CurrentStack
182 Stack stack
= (Stack
) Thread
.GetData(slot
);
187 Thread
.SetData(slot
, stack
);