Added RedirectUsingNamedRoute
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter / Attributes / DictionaryStringValuesAttribute.cs
blob799638bf4210c1b3d621d35d211b5179c1756d70
1 // Copyright 2004-2008 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.ComponentModel;
21 /// <summary>
22 /// Converts all properties to strings.
23 /// </summary>
24 [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Property,
25 AllowMultiple = false, Inherited = true)]
26 public class DictionaryStringValuesAttribute : DictionaryBehaviorAttribute, IDictionaryPropertySetter
28 private string format;
30 /// <summary>
31 /// Gets or sets the format.
32 /// </summary>
33 /// <value>The format.</value>
34 public string Format
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)
46 if (value != null)
48 value = GetPropertyAsString(property, value);
50 return true;
53 #endregion
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();