Create a PropertyDescriptor class to collection property information and simplify...
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter / DefaultPropertyGetter.cs
blob62b191446c1fd7703193d4fad65c4f4bbc86a9d1
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.Components.DictionaryAdapter
17 using System;
18 using System.Collections;
19 using System.ComponentModel;
21 /// <summary>
22 /// Manages conversion between property values.
23 /// </summary>
24 public class DefaultPropertyGetter : IDictionaryPropertyGetter
26 /// <summary>
27 /// Singleton instance of <see cref="DefaultPropertyGetter"/>
28 /// </summary>
29 public static readonly DefaultPropertyGetter
30 Instance = new DefaultPropertyGetter();
32 private DefaultPropertyGetter()
36 /// <summary>
37 /// Gets the effective dictionary value.
38 /// </summary>
39 /// <param name="factory">The dictionary factory.</param>
40 /// <param name="dictionary">The dictionary.</param>
41 /// <param name="key">The key.</param>
42 /// <param name="storedValue">The stored value.</param>
43 /// <param name="property">The property.</param>
44 /// <returns>The effective property value.</returns>
45 public object GetPropertyValue(IDictionaryAdapterFactory factory,
46 IDictionary dictionary, string key,
47 object storedValue, PropertyDescriptor property)
49 Type propertyType = property.Property.PropertyType;
51 if (storedValue != null && !propertyType.IsInstanceOfType(storedValue))
53 TypeConverter converter = TypeDescriptor.GetConverter(propertyType);
54 if (converter != null && converter.CanConvertFrom(storedValue.GetType()))
56 return converter.ConvertFrom(storedValue);
60 return storedValue;