@@ -183,6 +183,13 @@ static void CallConfigurationMethods(Dictionary<string, Dictionary<string, strin
183183 }
184184 }
185185
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+
186193 internal static object ConvertToType ( string value , Type toType )
187194 {
188195 var toTypeInfo = toType . GetTypeInfo ( ) ;
@@ -199,18 +206,34 @@ internal static object ConvertToType(string value, Type toType)
199206 if ( toTypeInfo . IsEnum )
200207 return Enum . Parse ( toType , value ) ;
201208
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
209210 . Where ( t => t . Key . GetTypeInfo ( ) . IsAssignableFrom ( toTypeInfo ) )
210211 . Select ( t => t . Value )
211212 . FirstOrDefault ( ) ;
212213
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 ) ;
214237 }
215238
216239 internal static IList < MethodInfo > FindSinkConfigurationMethods ( IEnumerable < Assembly > configurationAssemblies )
0 commit comments