Skip to content

Commit

Permalink
Added logic to automatically fill all generic (I)List / (I)Dictionary…
Browse files Browse the repository at this point in the history
… with the default value (string), removed some unneeded mapping for the TypeConverter(s).
  • Loading branch information
Lakritzator committed Aug 4, 2015
1 parent b957898 commit 7859bbe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Dapplo.Config.Test/TestInterfaces/IIniTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ string FirstName
set;
}

[DefaultValue("5,3,2,1,1"), TypeConverter(typeof (StringToGenericListConverter<int>))]
[DefaultValue("5,3,2,1,1")]
IList<int> WindowCornerCutShape
{
get;
Expand All @@ -69,7 +69,7 @@ string NotWritten
set;
}

[Description("Here are some values"), TypeConverter(typeof (GenericDictionaryConverter<string, int>))]
[Description("Here are some values")]
IDictionary<string, int> SomeValues
{
get;
Expand Down
13 changes: 1 addition & 12 deletions Dapplo.Config/Ini/IniConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,7 @@ public IniConfig SetDefaultConverters()
SetDefaultConverter(typeof(System.Drawing.Point), typeof(PointTypeConverter));
SetDefaultConverter(typeof(System.Drawing.Rectangle), typeof(RectangleTypeConverter));
SetDefaultConverter(typeof(System.Drawing.Color), typeof(ColorTypeConverter));

// Dictionary
SetDefaultConverter(typeof(IDictionary<string, string>), typeof(GenericDictionaryConverter<string, string>));
SetDefaultConverter(typeof(Dictionary<string, string>), typeof(GenericDictionaryConverter<string, string>));
SetDefaultConverter(typeof(IDictionary<string, int>), typeof(GenericDictionaryConverter<string, int>));
SetDefaultConverter(typeof(Dictionary<string, int>), typeof(GenericDictionaryConverter<string, int>));

// List
SetDefaultConverter(typeof(IList<string>), typeof(StringToGenericListConverter<string>));
SetDefaultConverter(typeof(List<string>), typeof(StringToGenericListConverter<string>));
SetDefaultConverter(typeof(IList<int>), typeof(StringToGenericListConverter<int>));
SetDefaultConverter(typeof(List<int>), typeof(StringToGenericListConverter<int>));

return this;
}

Expand Down
3 changes: 2 additions & 1 deletion Dapplo.Config/Support/PropertyInfoExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public static TypeConverter GetTypeConverter(this PropertyInfo propertyInfo) {
return (TypeConverter)Activator.CreateInstance(typeConverterType);
}
}
return null;

return propertyInfo.PropertyType.GetTypeConverter();
}

/// <summary>
Expand Down
20 changes: 19 additions & 1 deletion Dapplo.Config/Support/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using Dapplo.Config.Converters;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Dapplo.Config.Support {
/// <summary>
Expand All @@ -41,7 +43,7 @@ public static bool IsGenericDirectory(this Type valueType) {
/// </summary>
/// <param name="valueType">Type to check for</param>
/// <returns>true if it is a generic list</returns>
public static bool IsGenericList(this Type valueType) {
public static bool IsGenericList(this Type valueType) {
return valueType.IsGenericType && (valueType.GetGenericTypeDefinition() == typeof(List<>) || valueType.GetGenericTypeDefinition() == typeof(IList<>));
}

Expand All @@ -62,5 +64,21 @@ public static object CreateInstance(this Type valueType) {
}
return Activator.CreateInstance(valueType);
}

/// <summary>
/// Create a type converter for the supplied type
/// </summary>
/// <param name="valueType"></param>
/// <returns>TypeConverter</returns>
public static TypeConverter GetTypeConverter(this Type valueType) {
if (IsGenericList(valueType)) {
return (TypeConverter)Activator.CreateInstance(typeof(StringToGenericListConverter<>).MakeGenericType(valueType.GetGenericArguments()[0]));
} else if (IsGenericDirectory(valueType)) {
Type type1 = valueType.GetGenericArguments()[0];
Type type2 = valueType.GetGenericArguments()[1];
return (TypeConverter)Activator.CreateInstance(typeof(GenericDictionaryConverter<,>).MakeGenericType(type1, type2));
}
return null;
}
}
}

0 comments on commit 7859bbe

Please sign in to comment.