Added default constructor to PropertyDescriptor. This will normally be used to provi...
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter / Attributes / DictionaryAdapterPropertyBinder.cs
blobcc589dc81be89d251a36a66463168b50909ac08b
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.Reflection;
21 /// <summary>
22 /// Base class for property binders which defines the bidirectional conversion methods.
23 /// </summary>
24 public abstract class DictionaryAdapterPropertyBinder
26 /// <summary>
27 /// Performs the dictionary -> interface conversion for when client code is attempting to read out
28 /// of the "adapted" dictionary.
29 /// </summary>
30 /// <param name="value">The string value to convert to the property type.</param>
31 /// <returns>The converted value.</returns>
32 public object ConvertFromDictionary(object value)
34 AssertDictionaryValueHasExpectedType(value, DictionaryType);
35 return PerformConversionFromDictionary(value);
39 /// <summary>
40 /// Gets the type of the dictionary.
41 /// </summary>
42 /// <value>The type of the dictionary.</value>
43 protected abstract Type DictionaryType { get; }
45 /// <summary>
46 /// Performs the conversion from dictionary.
47 /// </summary>
48 /// <param name="value">The value.</param>
49 /// <returns></returns>
50 protected abstract object PerformConversionFromDictionary(object value);
52 /// <summary>
53 /// Performs the interface -> dictionary conversion for when the client code is attempting to write
54 /// into the "adapted" dictionary.
55 /// </summary>
56 /// <param name="value">The value to convert.</param>
57 /// <returns>The converted value.</returns>
58 public abstract object ConvertFromInterface(object value);
60 /// <summary>
61 /// Asserts that the type in the dictionary is the type expected, and if not, throws a meaningful
62 /// exception rather than InvalidCastException.
63 /// </summary>
64 /// <param name="value"></param>
65 /// <param name="interfaceType"></param>
66 protected void AssertDictionaryValueHasExpectedType(object value, Type interfaceType)
68 if (value.GetType() != interfaceType)
70 throw new DictionaryAdapterPropertyBindingException(GetType(), value, interfaceType);