I have this class CustomFileStream, below is its signature
public class CustomFileStream : IRandomAccessStream {}
But when I use it like this,
IAsyncOperation<IRandomAccessStream> IStorageFile.OpenAsync(FileAccessMode accessMode)
{
return Task.Factory.StartNew(() => new CustomFileStream()).AsAsyncOperation();
}
I get a compilation error,
Cannot implicitly convert type 'Windows.Foundation.IAsyncOperation<CustomFileStream>'
to
'Windows.Foundation.IAsyncOperation<Windows.Storage.Streams.IRandomAccessStream>'
Can someone please see if I am doing something wrong here.
THanks.
This isn't possible due to the fact that
IAsyncOperation<T>isn'tcovariant:A covaraint interface is declared with the
outmodifier added to it in the generic type declaration, so asIEnumerable<out T>.To get around your problem, you can explicitly cast the returned instance back to an
IRandomAccessStream:As a side note - using
Task.Factory.StartNewto imitate asynchoronous behavior is bad practice. You shouldn't expose async wrappers over sync method calls