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.
17 namespace Castle
.Components
.DictionaryAdapter
19 using System
.Collections
;
22 /// Allows the user to convert the values in the dictionary into a different type on access.
24 [AttributeUsage(AttributeTargets
.Property
, AllowMultiple
= false, Inherited
= true)]
25 public class DictionaryAdapterPropertyBinderAttribute
:
26 DictionaryBehaviorAttribute
, IDictionaryPropertyGetter
, IDictionaryPropertySetter
28 private readonly DictionaryAdapterPropertyBinder binder
;
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.
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
);
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);