@@ -183,6 +183,13 @@ static void CallConfigurationMethods(Dictionary<string, Dictionary<string, strin
183
183
}
184
184
}
185
185
186
+ static readonly Dictionary < Type , Func < string , object > > ExtendedTypeConversions = new Dictionary < Type , Func < string , object > >
187
+ {
188
+ { typeof ( Uri ) , s => new Uri ( s ) } ,
189
+ { typeof ( TimeSpan ) , s => TimeSpan . Parse ( s ) }
190
+ } ;
191
+
192
+
186
193
internal static object ConvertToType ( string value , Type toType )
187
194
{
188
195
var toTypeInfo = toType . GetTypeInfo ( ) ;
@@ -199,18 +206,34 @@ internal static object ConvertToType(string value, Type toType)
199
206
if ( toTypeInfo . IsEnum )
200
207
return Enum . Parse ( toType , value ) ;
201
208
202
- var extendedTypeConversions = new Dictionary < Type , Func < string , object > >
203
- {
204
- { typeof ( Uri ) , s => new Uri ( s ) } ,
205
- { typeof ( TimeSpan ) , s => TimeSpan . Parse ( s ) }
206
- } ;
207
-
208
- var convertor = extendedTypeConversions
209
+ var convertor = ExtendedTypeConversions
209
210
. Where ( t => t . Key . GetTypeInfo ( ) . IsAssignableFrom ( toTypeInfo ) )
210
211
. Select ( t => t . Value )
211
212
. FirstOrDefault ( ) ;
212
213
213
- return convertor == null ? Convert . ChangeType ( value , toType ) : convertor ( value ) ;
214
+ if ( convertor != null )
215
+ return convertor ( value ) ;
216
+
217
+ if ( toTypeInfo . IsInterface && ! string . IsNullOrWhiteSpace ( value ) )
218
+ {
219
+ var type = Type . GetType ( value . Trim ( ) , throwOnError : false ) ;
220
+ if ( type != null )
221
+ {
222
+ var ctor = type . GetTypeInfo ( ) . DeclaredConstructors . FirstOrDefault ( ci =>
223
+ {
224
+ var parameters = ci . GetParameters ( ) ;
225
+ return parameters . Length == 0 || parameters . All ( pi => pi . HasDefaultValue ) ;
226
+ } ) ;
227
+
228
+ if ( ctor == null )
229
+ throw new InvalidOperationException ( $ "A default constructor was not found on { type . FullName } .") ;
230
+
231
+ var call = ctor . GetParameters ( ) . Select ( pi => pi . DefaultValue ) . ToArray ( ) ;
232
+ return ctor . Invoke ( call ) ;
233
+ }
234
+ }
235
+
236
+ return Convert . ChangeType ( value , toType ) ;
214
237
}
215
238
216
239
internal static IList < MethodInfo > FindSinkConfigurationMethods ( IEnumerable < Assembly > configurationAssemblies )
0 commit comments