Example #1
0
        private static ClassDeclarationSyntax AddMediaDownloadMethods(PropertyDeclarationSyntax mediaDownloader, ClassDeclarationSyntax cls, SourceFileContext ctx)
        {
            var stream                       = Parameter(ctx.Type <Stream>(), "stream");
            var cancellationToken            = Parameter(ctx.Type <CancellationToken>(), "cancellationToken");
            var cancellationTokenWithDefault = Parameter(ctx.Type <CancellationToken>(), "cancellationToken", DefaultExpression(ctx.Type <CancellationToken>()));
            var range = Parameter(ctx.Type <RangeHeaderValue>(), "range");

            var syncDownloadWithStatus = Method(Modifier.Public | Modifier.Virtual, ctx.Type <IDownloadProgress>(), "DownloadWithStatus")(stream)
                                         .WithBlockBody(Return(mediaDownloader.Call("Download")(ThisQualifiedCall("GenerateRequestUri")(), stream)))
                                         .WithXmlDoc(
                XmlDoc.Summary("Synchronously download the media into the given stream."),
                XmlDoc.Returns("The final status of the download; including whether the download succeeded or failed."));

            var syncDownload = Method(Modifier.Public | Modifier.Virtual, ctx.Type(Typ.Void), "Download")(stream)
                               .WithBlockBody(mediaDownloader.Call("Download")(ThisQualifiedCall("GenerateRequestUri")(), stream))
                               .WithXmlDoc(XmlDoc.Summary(
                                               XmlDoc.Para("Synchronously download the media into the given stream."),
                                               XmlDoc.Para("Warning: This method hides download errors; use ", syncDownloadWithStatus, " instead.")));

            var asyncDownloadNoToken = Method(Modifier.Public | Modifier.Virtual, ctx.Type <Task <IDownloadProgress> >(), "DownloadAsync")(stream)
                                       .WithBlockBody(Return(mediaDownloader.Call("DownloadAsync")(ThisQualifiedCall("GenerateRequestUri")(), stream)))
                                       .WithXmlDoc(XmlDoc.Summary("Asynchronously download the media into the given stream."));

            var asyncDownloadWithToken = Method(Modifier.Public | Modifier.Virtual, ctx.Type <Task <IDownloadProgress> >(), "DownloadAsync")(stream, cancellationToken)
                                         .WithParameterLineBreaks()
                                         .WithBlockBody(Return(mediaDownloader.Call("DownloadAsync")(ThisQualifiedCall("GenerateRequestUri")(), stream, cancellationToken)))
                                         .WithXmlDoc(XmlDoc.Summary("Asynchronously download the media into the given stream."));

            var mediaDownloaderLocal = Local(ctx.Type(Typ.Var), "mediaDownloader")
                                       .WithInitializer(New(ctx.Type <MediaDownloader>())(Property(0, ctx.Type <IClientService>(), "Service")));
            var syncRange = Method(Modifier.Public | Modifier.Virtual, ctx.Type <IDownloadProgress>(), "DownloadRange")(stream, range)
                            .WithBlockBody(
                mediaDownloaderLocal,
                mediaDownloaderLocal.Access("Range").Assign(range),
                Return(mediaDownloaderLocal.Call("Download")(ThisQualifiedCall("GenerateRequestUri")(), stream)))
                            .WithXmlDoc(XmlDoc.Summary("Synchronously download a range of the media into the given stream."));

            var asyncRange = Method(Modifier.Public | Modifier.Virtual, ctx.Type <Task <IDownloadProgress> >(), "DownloadRangeAsync")(stream, range, cancellationTokenWithDefault)
                             .WithParameterLineBreaks()
                             .WithBlockBody(
                mediaDownloaderLocal,
                mediaDownloaderLocal.Access("Range").Assign(range),
                Return(mediaDownloaderLocal.Call("DownloadAsync")(ThisQualifiedCall("GenerateRequestUri")(), stream, cancellationTokenWithDefault)))
                             .WithXmlDoc(XmlDoc.Summary("Asynchronously download a range of the media into the given stream."));

            return(cls.AddMembers(syncDownload, syncDownloadWithStatus, asyncDownloadNoToken, asyncDownloadWithToken, syncRange, asyncRange));
        }