public async Task <IValueProvider> BindAsync(object value, ValueBindingContext context) { string path = (string)value; IFileStreamProvider strategy = await GetStreamHelper(path); var outTuple = await strategy.OpenWriteStreamAsync(); Stream outStream = outTuple.Item1; Func <object, Task> completedFunc = outTuple.Item2; // Bind to 'out string' var valueProvider = new GenericOutFileValueProvider(outStream, completedFunc); return(valueProvider); }
public async Task <IValueProvider> BindAsync(object value, ValueBindingContext context) { string path = (string)value; IFileStreamProvider strategy = await GetStreamHelper(path); var targetType = _parameter.ParameterType; object userObj = null; Func <object, Task> onComplete; ConstantObj valueProvider = null; if (_source.Access == FileAccess.Write) { var tuple = await strategy.OpenWriteStreamAsync(); var writeStream = tuple.Item1; var saveStreamFunc = tuple.Item2; if (targetType == typeof(Stream) || (_parameter.IsOut && targetType == typeof(Stream).MakeByRefType())) { userObj = writeStream; onComplete = saveStreamFunc; } else if (targetType == typeof(TextWriter) || (_parameter.IsOut && targetType == typeof(TextWriter).MakeByRefType())) { var tw = new StreamWriter(writeStream); userObj = tw; onComplete = async obj => { tw.Flush(); await saveStreamFunc(obj); }; } else if (targetType == typeof(StreamWriter) || (_parameter.IsOut && targetType == typeof(StreamWriter).MakeByRefType())) { var tw = new StreamWriter(writeStream); userObj = tw; onComplete = async obj => { tw.Flush(); await saveStreamFunc(obj); }; } else if (targetType == typeof(byte[]) || (_parameter.IsOut && targetType == typeof(byte[]).MakeByRefType())) { userObj = writeStream; onComplete = saveStreamFunc; } else { throw new InvalidOperationException("Unsupported type:" + targetType.FullName); } } else { var readStream = await strategy.OpenReadStreamAsync(); onComplete = obj => Task.FromResult(0); // Nop if (targetType == typeof(Stream)) { userObj = readStream; } else if (targetType == typeof(TextReader)) { userObj = new StreamReader(readStream); } else if (targetType == typeof(StreamReader)) { userObj = new StreamReader(readStream); } else if (targetType == typeof(string)) { userObj = new StreamReader(readStream).ReadToEnd(); } else if (targetType == typeof(byte[]) && readStream is MemoryStream) { userObj = ((MemoryStream)readStream).ToArray(); } else { throw new InvalidOperationException("Unsupported type:" + targetType.FullName); } } valueProvider = new ConstantObj { Type = targetType, Value = userObj, OnCompleted = onComplete }; return(valueProvider); }