1 // Copyright 2004-2008 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
.ComponentModel
;
22 /// Converts all properties to strings.
24 [AttributeUsage(AttributeTargets
.Interface
| AttributeTargets
.Property
,
25 AllowMultiple
= false, Inherited
= true)]
26 public class DictionaryStringValuesAttribute
: DictionaryBehaviorAttribute
, IDictionaryPropertySetter
28 private string format
;
31 /// Gets or sets the format.
33 /// <value>The format.</value>
36 get { return format; }
37 set { format = value; }
40 #region IDictionaryPropertySetter Members
42 bool IDictionaryPropertySetter
.SetPropertyValue(
43 IDictionaryAdapterFactory factory
, IDictionary dictionary
,
44 string key
, ref object value, PropertyDescriptor property
)
48 value = GetPropertyAsString(property
, value);
55 private string GetPropertyAsString(PropertyDescriptor property
, object value)
57 if (!string.IsNullOrEmpty(format
))
59 return String
.Format(format
, value);
62 TypeConverter converter
= property
.TypeConverter
;
65 if (converter
!= null && converter
.CanConvertTo(typeof(string)))
67 return (string) converter
.ConvertTo(value, typeof(string));
70 return value.ToString();