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
.Components
.DictionaryAdapter
18 using System
.Collections
;
19 using System
.Reflection
;
22 /// Base class for property binders which defines the bidirectional conversion methods.
24 public abstract class DictionaryAdapterPropertyBinder
27 /// Performs the dictionary -> interface conversion for when client code is attempting to read out
28 /// of the "adapted" dictionary.
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);
40 /// Gets the type of the dictionary.
42 /// <value>The type of the dictionary.</value>
43 protected abstract Type DictionaryType { get; }
46 /// Performs the conversion from dictionary.
48 /// <param name="value">The value.</param>
49 /// <returns></returns>
50 protected abstract object PerformConversionFromDictionary(object value);
53 /// Performs the interface -> dictionary conversion for when the client code is attempting to write
54 /// into the "adapted" dictionary.
56 /// <param name="value">The value to convert.</param>
57 /// <returns>The converted value.</returns>
58 public abstract object ConvertFromInterface(object value);
61 /// Asserts that the type in the dictionary is the type expected, and if not, throws a meaningful
62 /// exception rather than InvalidCastException.
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
);