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
.Components
.DictionaryAdapter
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
20 using System
.ComponentModel
;
21 using System
.Reflection
;
24 /// Describes a dictionary property.
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
;
37 /// Initializes an empty <see cref="PropertyDescriptor"/> class.
39 public PropertyDescriptor()
44 /// Initializes a new instance of the <see cref="PropertyDescriptor"/> class.
46 /// <param name="property">The property.</param>
47 public PropertyDescriptor(PropertyInfo property
)
49 this.property
= property
;
53 /// Initializes a new instance of the <see cref="PropertyDescriptor"/> class
54 /// with an existing property descriptor.
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
);
68 public int ExecutionOrder
74 /// Gets the property name.
76 public string PropertyName
78 get { return (Property != null) ? Property.Name : null; }
82 /// Gets the property type.
84 public Type PropertyType
86 get { return (Property != null) ? Property.PropertyType : null; }
90 /// Gets the property.
92 /// <value>The property.</value>
93 public PropertyInfo Property
95 get { return property; }
99 /// Gets the type converter.
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
);
116 typeConverter
= TypeDescriptor
.GetConverter(PropertyType
);
120 return typeConverter
;
125 /// Gets or sets the key builders.
127 /// <value>The key builders.</value>
128 public ICollection
<IDictionaryKeyBuilder
> KeyBuilders
130 get { return keyBuilders; }
134 /// Gets or sets the setter.
136 /// <value>The setter.</value>
137 public ICollection
<IDictionaryPropertySetter
> Setters
139 get { return setters; }
143 /// Gets or sets the getter.
145 /// <value>The getter.</value>
146 public ICollection
<IDictionaryPropertyGetter
> Getters
148 get { return getters; }
151 #region IDictionaryKeyBuilder Members
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);
183 /// Adds the key builder.
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
);
196 /// Adds the key builders.
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
);
209 keyBuilders
.AddRange(builders
);
216 #region IDictionaryPropertyGetter Members
219 /// Gets the property value.
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
)
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);
253 /// Adds the dictionary getter.
255 /// <param name="getter">The getter.</param>
256 public void AddGetter(IDictionaryPropertyGetter getter
)
260 getters
= new List
<IDictionaryPropertyGetter
>();
266 /// Adds the dictionary getters.
268 /// <param name="gets">The getters.</param>
269 public void AddGetters(ICollection
<IDictionaryPropertyGetter
> gets
)
275 getters
= new List
<IDictionaryPropertyGetter
>(gets
);
279 getters
.AddRange(gets
);
286 #region IDictionaryPropertySetter Members
289 /// Sets the property value.
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;
305 foreach(IDictionaryPropertySetter setter
in setters
)
307 if (!setter
.SetPropertyValue(
308 factory
, dictionary
, key
, ref value, this))
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))
331 /// Adds the dictionary setter.
333 /// <param name="setter">The setter.</param>
334 public void AddSetter(IDictionaryPropertySetter setter
)
338 setters
= new List
<IDictionaryPropertySetter
>();
344 /// Adds the dictionary setters.
346 /// <param name="sets">The setters.</param>
347 public void AddSetters(ICollection
<IDictionaryPropertySetter
> sets
)
353 setters
= new List
<IDictionaryPropertySetter
>(sets
);
357 setters
.AddRange(sets
);