From b6843e2a9b32c7ea566b6a2fc11e66400abdd9c3 Mon Sep 17 00:00:00 2001 From: cneuwirt Date: Mon, 19 Nov 2007 10:42:47 +0000 Subject: [PATCH] Added bidirectional support for DictionaryStringListAttribute.cs so you can assign IEnumerable to property and have string assigned intermally. git-svn-id: https://svn.castleproject.org/svn/castle/trunk@4497 73e77b4c-caa6-f847-a29a-24ab75ae54b6 --- .../DictionaryAdapterFactoryTestCase.cs | 5 +- .../Attributes/DictionaryStringListAttribute.cs | 64 +++++++++++++++------- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/Components/DictionaryAdapter/Castle.Components.DictionaryAdapter.Tests/DictionaryAdapterFactoryTestCase.cs b/Components/DictionaryAdapter/Castle.Components.DictionaryAdapter.Tests/DictionaryAdapterFactoryTestCase.cs index e2937deed..286b9e322 100644 --- a/Components/DictionaryAdapter/Castle.Components.DictionaryAdapter.Tests/DictionaryAdapterFactoryTestCase.cs +++ b/Components/DictionaryAdapter/Castle.Components.DictionaryAdapter.Tests/DictionaryAdapterFactoryTestCase.cs @@ -533,12 +533,13 @@ namespace Castle.Components.DictionaryAdapter.Tests Assert.AreEqual("Craig,Brenda,Kaitlyn,Lauren,Matthew", dictionary["Names"]); - IList ages = lists.Ages; + IList ages = new List(); ages.Add(37); ages.Add(36); ages.Add(5); ages.Add(3); ages.Add(1); + lists.Ages = ages; Assert.AreEqual("37,36,5,3,1", dictionary["Ages"]); } @@ -753,6 +754,6 @@ namespace Castle.Components.DictionaryAdapter.Tests IList Names { get; } [DictionaryStringList] - IList Ages { get; } + IList Ages { get; set; } } } \ No newline at end of file diff --git a/Components/DictionaryAdapter/Castle.Components.DictionaryAdapter/Attributes/DictionaryStringListAttribute.cs b/Components/DictionaryAdapter/Castle.Components.DictionaryAdapter/Attributes/DictionaryStringListAttribute.cs index 41fa3b7a7..c5abf0566 100644 --- a/Components/DictionaryAdapter/Castle.Components.DictionaryAdapter/Attributes/DictionaryStringListAttribute.cs +++ b/Components/DictionaryAdapter/Castle.Components.DictionaryAdapter/Attributes/DictionaryStringListAttribute.cs @@ -25,7 +25,9 @@ namespace Castle.Components.DictionaryAdapter /// delimited string value. /// [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] - public class DictionaryStringListAttribute : Attribute, IDictionaryPropertyGetter + public class DictionaryStringListAttribute : Attribute, + IDictionaryPropertyGetter, + IDictionaryPropertySetter { private char separator = ','; @@ -64,7 +66,7 @@ namespace Castle.Components.DictionaryAdapter if (converter != null && converter.CanConvertFrom(typeof(string))) { Type genericList = typeof(StringListWrapper<>).MakeGenericType( - new Type[] { paramType }); + new Type[] {paramType}); return Activator.CreateInstance(genericList, key, storedValue, separator, dictionary); } @@ -76,6 +78,44 @@ namespace Castle.Components.DictionaryAdapter } #endregion + + #region IDictionaryPropertySetter Members + + object IDictionaryPropertySetter.SetPropertyValue( + IDictionaryAdapterFactory factory, IDictionary dictionary, + string key, object value, PropertyDescriptor property) + { + IEnumerable enumerable = value as IEnumerable; + if (enumerable != null) + { + value = BuildString(enumerable, separator); + } + return value; + } + + #endregion + + internal static string BuildString(IEnumerable enumerable, char separator) + { + bool first = true; + StringBuilder builder = new StringBuilder(); + + foreach(object item in enumerable) + { + if (first) + { + first = false; + } + else + { + builder.Append(separator); + } + + builder.Append(item.ToString()); + } + + return builder.ToString(); + } } #region StringList @@ -208,24 +248,8 @@ namespace Castle.Components.DictionaryAdapter private void SynchronizeDictionary() { - bool first = true; - StringBuilder builder = new StringBuilder(); - - foreach(T item in inner) - { - if (first) - { - first = false; - } - else - { - builder.Append(separator); - } - - builder.Append(item.ToString()); - } - - dictionary[key] = builder.ToString(); + dictionary[key] = DictionaryStringListAttribute. + BuildString(inner, separator); } } -- 2.11.4.GIT