Added RedirectUsingNamedRoute
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter / PropertyDescriptor.cs
blobb622eb60fca21a4ba3e87dab601dbae7a91f5697
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.Components.DictionaryAdapter
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Reflection;
23 /// <summary>
24 /// Describes a dictionary property.
25 /// </summary>
26 public class PropertyDescriptor : IDictionaryKeyBuilder,
27 IDictionaryPropertyGetter,
28 IDictionaryPropertySetter
30 private readonly PropertyInfo property;
31 private List<IDictionaryPropertyGetter> getters;
32 private List<IDictionaryPropertySetter> setters;
33 private List<IDictionaryKeyBuilder> keyBuilders;
34 private TypeConverter typeConverter;
36 /// <summary>
37 /// Initializes an empty <see cref="PropertyDescriptor"/> class.
38 /// </summary>
39 public PropertyDescriptor()
43 /// <summary>
44 /// Initializes a new instance of the <see cref="PropertyDescriptor"/> class.
45 /// </summary>
46 /// <param name="property">The property.</param>
47 public PropertyDescriptor(PropertyInfo property)
49 this.property = property;
52 /// <summary>
53 /// Initializes a new instance of the <see cref="PropertyDescriptor"/> class
54 /// with an existing property descriptor.
55 /// </summary>
56 /// <param name="other">The other descriptor.</param>
57 public PropertyDescriptor(PropertyDescriptor other)
59 property = other.property;
60 AddKeyBuilders(other.keyBuilders);
61 AddGetters(other.getters);
62 AddSetters(other.setters);
65 /// <summary>
66 ///
67 /// </summary>
68 public int ExecutionOrder
70 get { return 0; }
73 /// <summary>
74 /// Gets the property name.
75 /// </summary>
76 public string PropertyName
78 get { return (Property != null) ? Property.Name : null; }
81 /// <summary>
82 /// Gets the property type.
83 /// </summary>
84 public Type PropertyType
86 get { return (Property != null) ? Property.PropertyType : null; }
89 /// <summary>
90 /// Gets the property.
91 /// </summary>
92 /// <value>The property.</value>
93 public PropertyInfo Property
95 get { return property; }
98 /// <summary>
99 /// Gets the type converter.
100 /// </summary>
101 /// <value>The type converter.</value>
102 public TypeConverter TypeConverter
106 if (typeConverter == null)
108 Type converterType = AttributesUtil.GetTypeConverter(property);
110 if (converterType != null)
112 typeConverter = (TypeConverter) Activator.CreateInstance(converterType);
114 else
116 typeConverter = TypeDescriptor.GetConverter(PropertyType);
120 return typeConverter;
124 /// <summary>
125 /// Gets or sets the key builders.
126 /// </summary>
127 /// <value>The key builders.</value>
128 public ICollection<IDictionaryKeyBuilder> KeyBuilders
130 get { return keyBuilders; }
133 /// <summary>
134 /// Gets or sets the setter.
135 /// </summary>
136 /// <value>The setter.</value>
137 public ICollection<IDictionaryPropertySetter> Setters
139 get { return setters; }
142 /// <summary>
143 /// Gets or sets the getter.
144 /// </summary>
145 /// <value>The getter.</value>
146 public ICollection<IDictionaryPropertyGetter> Getters
148 get { return getters; }
151 #region IDictionaryKeyBuilder Members
153 /// <summary>
154 /// Gets the key.
155 /// </summary>
156 /// <param name="dictionary">The dictionary.</param>
157 /// <param name="key">The key.</param>
158 /// <param name="descriptor">The descriptor.</param>
159 /// <returns></returns>
160 public string GetKey(IDictionary dictionary, string key,
161 PropertyDescriptor descriptor)
163 if (keyBuilders != null)
165 foreach(IDictionaryKeyBuilder builder in keyBuilders)
167 key = builder.GetKey(dictionary, key, this);
171 if (descriptor != null && descriptor.KeyBuilders != null)
173 foreach (IDictionaryKeyBuilder builder in descriptor.KeyBuilders)
175 key = builder.GetKey(dictionary, key, this);
179 return key;
182 /// <summary>
183 /// Adds the key builder.
184 /// </summary>
185 /// <param name="builder">The builder.</param>
186 public void AddKeyBuilder(IDictionaryKeyBuilder builder)
188 if (keyBuilders == null)
190 keyBuilders = new List<IDictionaryKeyBuilder>();
192 keyBuilders.Add(builder);
195 /// <summary>
196 /// Adds the key builders.
197 /// </summary>
198 /// <param name="builders">The builders.</param>
199 public void AddKeyBuilders(ICollection<IDictionaryKeyBuilder> builders)
201 if (builders != null)
203 if (keyBuilders == null)
205 keyBuilders = new List<IDictionaryKeyBuilder>(builders);
207 else
209 keyBuilders.AddRange(builders);
214 #endregion
216 #region IDictionaryPropertyGetter Members
218 /// <summary>
219 /// Gets the property value.
220 /// </summary>
221 /// <param name="factory">The factory.</param>
222 /// <param name="dictionary">The dictionary.</param>
223 /// <param name="key">The key.</param>
224 /// <param name="storedValue">The stored value.</param>
225 /// <param name="descriptor">The descriptor.</param>
226 /// <returns></returns>
227 public object GetPropertyValue(
228 IDictionaryAdapterFactory factory, IDictionary dictionary,
229 string key, object storedValue, PropertyDescriptor descriptor)
231 if (getters != null)
233 foreach(IDictionaryPropertyGetter getter in getters)
235 storedValue = getter.GetPropertyValue(
236 factory, dictionary, key, storedValue, this);
240 if (descriptor != null && descriptor.Getters != null)
242 foreach (IDictionaryPropertyGetter getter in descriptor.Getters)
244 storedValue = getter.GetPropertyValue(
245 factory, dictionary, key, storedValue, this);
249 return storedValue;
252 /// <summary>
253 /// Adds the dictionary getter.
254 /// </summary>
255 /// <param name="getter">The getter.</param>
256 public void AddGetter(IDictionaryPropertyGetter getter)
258 if (getters == null)
260 getters = new List<IDictionaryPropertyGetter>();
262 getters.Add(getter);
265 /// <summary>
266 /// Adds the dictionary getters.
267 /// </summary>
268 /// <param name="gets">The getters.</param>
269 public void AddGetters(ICollection<IDictionaryPropertyGetter> gets)
271 if (gets != null)
273 if (getters == null)
275 getters = new List<IDictionaryPropertyGetter>(gets);
277 else
279 getters.AddRange(gets);
284 #endregion
286 #region IDictionaryPropertySetter Members
288 /// <summary>
289 /// Sets the property value.
290 /// </summary>
291 /// <param name="factory">The factory.</param>
292 /// <param name="dictionary">The dictionary.</param>
293 /// <param name="key">The key.</param>
294 /// <param name="value">The value.</param>
295 /// <param name="descriptor">The descriptor.</param>
296 /// <returns></returns>
297 public bool SetPropertyValue(
298 IDictionaryAdapterFactory factory, IDictionary dictionary,
299 string key, ref object value, PropertyDescriptor descriptor)
301 bool consumed = false;
303 if (setters != null)
305 foreach(IDictionaryPropertySetter setter in setters)
307 if (!setter.SetPropertyValue(
308 factory, dictionary, key, ref value, this))
310 consumed = true;
315 if (descriptor != null && descriptor.Setters != null)
317 foreach (IDictionaryPropertySetter setter in descriptor.Setters)
319 if (!setter.SetPropertyValue(
320 factory, dictionary, key, ref value, this))
322 consumed = true;
327 return !consumed;
330 /// <summary>
331 /// Adds the dictionary setter.
332 /// </summary>
333 /// <param name="setter">The setter.</param>
334 public void AddSetter(IDictionaryPropertySetter setter)
336 if (setters == null)
338 setters = new List<IDictionaryPropertySetter>();
340 setters.Add(setter);
343 /// <summary>
344 /// Adds the dictionary setters.
345 /// </summary>
346 /// <param name="sets">The setters.</param>
347 public void AddSetters(ICollection<IDictionaryPropertySetter> sets)
349 if (sets != null)
351 if (setters == null)
353 setters = new List<IDictionaryPropertySetter>(sets);
355 else
357 setters.AddRange(sets);
362 #endregion