public object ReadFrom(IHttpEntity request, IType destinationType, string parameterName) { if (destinationType.IsAssignableFrom <IEnumerable <IMultipartHttpEntity> >()) { var multipartReader = new MultipartReader(request.ContentType.Boundary, request.Stream); return(multipartReader.GetParts()); } if (destinationType.IsAssignableFrom <IDictionary <string, IList <IMultipartHttpEntity> > >()) { return(FormData(request)); } var binder = BinderLocator.GetBinder(destinationType); if (binder == null) { throw new InvalidOperationException("Cannot find a binder to create the object"); } binder.Prefixes.Add(parameterName); bool wasAnyKeyUsed = ReadKeyValues(request).Aggregate(false, (wasUsed, kv) => kv.SetProperty(binder) || wasUsed); var result = binder.BuildObject(); return(wasAnyKeyUsed && result.Successful ? result.Instance : Missing.Value); }
public BindingResult ConvertValues(IMultipartHttpEntity entity, Type targetType) { object destination; var sourceMediaType = entity.ContentType ?? MediaType.TextPlain; var type = _typeSystem.FromClr(targetType); var mediaTypeReaderReg = _codecs.FindMediaTypeReader(sourceMediaType, new[] { type }, null); if (mediaTypeReaderReg != null) { var mediaTypeReader = (ICodec)_container.Resolve(mediaTypeReaderReg.CodecRegistration.CodecType); if (mediaTypeReader is IMediaTypeReader) { return(BindingResult.Success(((IMediaTypeReader)mediaTypeReader).ReadFrom(entity, type, targetType.Name))); } var binder = BinderLocator.GetBinder(type); if (mediaTypeReader.TryAssignKeyValues(entity, binder)) { return(binder.BuildObject()); } } // if no media type reader was found, try to parse to a string and convert from that. var stringType = _typeSystem.FromClr <string>(); mediaTypeReaderReg = _codecs.FindMediaTypeReader(sourceMediaType, new[] { stringType }, null); if (entity.ContentType == null) { entity.ContentType = MediaType.TextPlain; } // defaults the entity to UTF-8 if none is specified, to account for browsers favouring using the charset of the origin page rather than the standard. Cause RFCs are too difficult to follow uh... if (entity.ContentType.CharSet == null) { entity.ContentType.CharSet = "UTF-8"; } var plainTextReader = (IMediaTypeReader)_container.Resolve(mediaTypeReaderReg.CodecRegistration.CodecType); var targetString = plainTextReader.ReadFrom(entity, stringType, targetType.Name); destination = targetType.CreateInstanceFrom(targetString); return(BindingResult.Success(destination)); }