This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
CollectionConverter.cs
152 lines (119 loc) · 5.79 KB
/
CollectionConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
namespace Nancy.ModelBinding.DefaultConverters
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
/// <summary>
/// Converter for handling enumerable types
/// </summary>
public class CollectionConverter : ITypeConverter
{
private readonly MethodInfo enumerableCastMethod = typeof(Enumerable).GetMethod("Cast", BindingFlags.Public | BindingFlags.Static);
private readonly MethodInfo enumerableToArrayMethod = typeof(Enumerable).GetMethod("ToArray", BindingFlags.Public | BindingFlags.Static);
private readonly MethodInfo enumerableAsEnumerableMethod = typeof(Enumerable).GetMethod("AsEnumerable", BindingFlags.Public | BindingFlags.Static);
/// <summary>
/// Whether the converter can convert to the destination type
/// </summary>
/// <param name="destinationType">Destination type</param>
/// <returns>True if conversion supported, false otherwise</returns>
public bool CanConvertTo(Type destinationType)
{
return typeof(IEnumerable).IsAssignableFrom(destinationType);
}
/// <summary>
/// Convert the string representation to the destination type
/// </summary>
/// <param name="input">Input string</param>
/// <param name="destinationType">Destination type</param>
/// <param name="context">Current context</param>
/// <returns>Converted object of the destination type</returns>
public object Convert(string input, Type destinationType, BindingContext context)
{
// TODO - Lots of reflection in here, should probably cache the methodinfos
if (string.IsNullOrEmpty(input))
{
return null;
}
var items = input.Split(',');
if (IsCollection(destinationType))
{
return ConvertCollection(items, destinationType, context);
}
if (IsArray(destinationType))
{
return ConvertArray(items, destinationType, context);
}
if (IsEnumerable(destinationType))
{
return ConvertEnumerable(items, destinationType, context);
}
return null;
}
private bool IsCollection(Type destinationType)
{
var collectionType = typeof(ICollection<>);
return destinationType.IsGenericType && destinationType.GetInterfaces().
Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == collectionType).Any();
}
private bool IsArray(Type destinationType)
{
return destinationType.BaseType == typeof(Array);
}
private bool IsEnumerable(Type destinationType)
{
var enumerableType = typeof(IEnumerable<>);
return destinationType.IsGenericType && destinationType.GetGenericTypeDefinition() == enumerableType;
}
private object ConvertCollection(string[] items, Type destinationType, BindingContext context)
{
var genericType = destinationType.GetGenericArguments().First();
var returnCollection = Activator.CreateInstance(destinationType);
var converter = context.TypeConverters.Where(c => c.CanConvertTo(genericType)).FirstOrDefault();
if (converter == null)
{
return null;
}
var collectionAddMethod = destinationType.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
foreach (var item in items)
{
collectionAddMethod.Invoke(returnCollection, new[] { converter.Convert(item, genericType, context) });
}
return returnCollection;
}
private object ConvertArray(string[] items, Type destinationType, BindingContext context)
{
var elementType = destinationType.GetElementType();
if (elementType == null)
{
return null;
}
var converter = context.TypeConverters.Where(c => c.CanConvertTo(elementType)).FirstOrDefault();
if (converter == null)
{
return null;
}
var returnArray = items.Select(s => converter.Convert(s, elementType, context));
var genericCastMethod = this.enumerableCastMethod.MakeGenericMethod(new[] { elementType });
var generictoArrayMethod = this.enumerableToArrayMethod.MakeGenericMethod(new[] { elementType });
var castArray = genericCastMethod.Invoke(null, new object[] { returnArray });
return generictoArrayMethod.Invoke(null, new[] { castArray });
}
private object ConvertEnumerable(string[] items, Type destinationType, BindingContext context)
{
var genericType = destinationType.GetGenericArguments().First();
var converter = context.TypeConverters.Where(c => c.CanConvertTo(genericType)).FirstOrDefault();
if (converter == null)
{
return null;
}
var returnArray = items.Select(s => converter.Convert(s, genericType, context));
var genericCastMethod = this.enumerableCastMethod.MakeGenericMethod(new[] { genericType });
var genericAsEnumerableMethod = this.enumerableAsEnumerableMethod.MakeGenericMethod(new[] { genericType });
var castArray = genericCastMethod.Invoke(null, new object[] { returnArray });
return genericAsEnumerableMethod.Invoke(null, new[] { castArray });
}
}
}