Added default constructor to PropertyDescriptor. This will normally be used to provi...
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter / Attributes / DictionaryAdapterPropertyBinderAttribute.cs
blob28e6f17fd03d25c7a4a907223cd879e14a2f21a9
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 using System;
17 namespace Castle.Components.DictionaryAdapter
19 using System.Collections;
21 /// <summary>
22 /// Allows the user to convert the values in the dictionary into a different type on access.
23 /// </summary>
24 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
25 public class DictionaryAdapterPropertyBinderAttribute :
26 DictionaryBehaviorAttribute, IDictionaryPropertyGetter, IDictionaryPropertySetter
28 private readonly DictionaryAdapterPropertyBinder binder;
30 /// <summary>
31 /// Specifies a binder to perform conversion between the type currently stored in the
32 /// adapted dictionary, and the type the client code wishes to use via the interface.
33 /// </summary>
34 /// <param name="binderType"></param>
35 public DictionaryAdapterPropertyBinderAttribute(Type binderType)
37 if (!typeof(DictionaryAdapterPropertyBinder).IsAssignableFrom(binderType))
38 throw new ArgumentException("You may only supply DictionaryAdapterPropertyBinder types to the DictionaryAdapterPropertyBinderAttribute.");
40 binder = (DictionaryAdapterPropertyBinder) Activator.CreateInstance(binderType);
43 #region IDictionaryPropertyGetter
45 object IDictionaryPropertyGetter.GetPropertyValue(
46 IDictionaryAdapterFactory factory, IDictionary dictionary,
47 string key, object storedValue, PropertyDescriptor property)
49 return binder.ConvertFromDictionary(storedValue);
52 #endregion
54 #region IDictionaryPropertySetter
56 bool IDictionaryPropertySetter.SetPropertyValue(
57 IDictionaryAdapterFactory factory, IDictionary dictionary,
58 string key, ref object value, PropertyDescriptor property)
60 value = binder.ConvertFromInterface(value);
61 return true;
64 #endregion