private static Func <object[], object> GetCreator(Type type) { Func <object> defaultConstructor = ReflectionUtils.HasDefaultConstructor(type, false) ? ReflectionDelegateFactory.CreateDefaultConstructor <object>(type) : null; return(delegate(object[] parameters) { object obj2; try { if (parameters != null) { if (< > c.< > 9__20_1 == null) { } Type[] types = parameters.Select <object, Type>((< > c.< > 9__20_1 = new Func <object, Type>(< > c.< > 9. < GetCreator > b__20_1))).ToArray <Type>(); ConstructorInfo constructor = type.GetConstructor(types); if (null == constructor) { throw new JsonException("No matching parameterized constructor found for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type)); } return ReflectionDelegateFactory.CreateParameterizedConstructor(constructor)(parameters); } if (defaultConstructor == null) { throw new JsonException("No parameterless constructor defined for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type)); } obj2 = defaultConstructor(); } catch (Exception exception) { throw new JsonException("Error creating '{0}'.".FormatWith(CultureInfo.InvariantCulture, type), exception); } return obj2; }); }
private static Func <object[], object> GetCreator(Type type) { Func <object> defaultConstructor = ReflectionUtils.HasDefaultConstructor(type, false) ? ReflectionDelegateFactory.CreateDefaultConstructor <object>(type) : null; return((parameters) => { try { if (parameters != null) { Type[] paramTypes = parameters.Select(param => { if (param == null) { throw new InvalidOperationException("Cannot pass a null parameter to the constructor."); } return param.GetType(); }).ToArray(); ConstructorInfo parameterizedConstructorInfo = type.GetConstructor(paramTypes); if (parameterizedConstructorInfo != null) { ObjectConstructor <object> parameterizedConstructor = ReflectionDelegateFactory.CreateParameterizedConstructor(parameterizedConstructorInfo); return parameterizedConstructor(parameters); } else { throw new JsonException("No matching parameterized constructor found for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type)); } } if (defaultConstructor == null) { throw new JsonException("No parameterless constructor defined for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type)); } return defaultConstructor(); } catch (Exception ex) { throw new JsonException("Error creating '{0}'.".FormatWith(CultureInfo.InvariantCulture, type), ex); } }); }
/// <summary> /// Create a factory function that can be used to create instances of a JsonConverter described by the /// argument type. The returned function can then be used to either invoke the converter's default ctor, or any /// parameterized constructors by way of an object array. /// </summary> private static Func <object[], JsonConverter> GetJsonConverterCreator(Type converterType) { Func <object> defaultConstructor = (ReflectionUtils.HasDefaultConstructor(converterType, false)) ? ReflectionDelegateFactory.CreateDefaultConstructor <object>(converterType) : null; return((parameters) => { try { if (parameters != null) { ObjectConstructor <object> parameterizedConstructor = null; Type[] paramTypes = parameters.Select(param => param.GetType()).ToArray(); ConstructorInfo parameterizedConstructorInfo = converterType.GetConstructor(paramTypes); if (null != parameterizedConstructorInfo) { parameterizedConstructor = ReflectionDelegateFactory.CreateParameterizedConstructor(parameterizedConstructorInfo); return (JsonConverter)parameterizedConstructor(parameters); } else { throw new JsonException("No matching parameterized constructor found for '{0}'.".FormatWith(CultureInfo.InvariantCulture, converterType)); } } if (defaultConstructor == null) { throw new JsonException("No parameterless constructor defined for '{0}'.".FormatWith(CultureInfo.InvariantCulture, converterType)); } return (JsonConverter)defaultConstructor(); } catch (Exception ex) { throw new JsonException("Error creating '{0}'.".FormatWith(CultureInfo.InvariantCulture, converterType), ex); } }); }