在递归方法中使用Yield返回Try-Catch

时间:2018-02-08 09:05:54

标签: c# methods try-catch ienumerable yield-return

我需要在递归方法中实现try catch,但我无法弄清楚如何做到这一点。这是代码:

    private IEnumerable <FileItem> FilesToDownload(FileItem file)
    {
        logger = LogManager.GetLogger(GetType());

        using (var wb = new WebDavSession(webDavUrl, new NetworkCredential(user, psw)))
        using (Task<IList<WebDavSessionItem>> task = wb.ListAsync(file.Path))
        {
            task.Wait();
            foreach (var item in task.Result)
            {
                FileItem retFile = item.ToFileItem();
                logger.Info("Going Into " + retFile.Path);
                if (item.IsFolder == true)  
                {
                    foreach (var inner in FilesToDownload(retFile))
                    {
                        yield return inner;
                    }
                }
                else
                {
                    yield return retFile;
                }
            }
        } 
    }

这种方法可以帮助我找到嵌套文件夹(在云端)中的文件,因此递归是必要的。

你有什么建议吗?

1 个答案:

答案 0 :(得分:1)

无法做的事情是<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/plain text/html AddOutputFilterByType DEFLATE text/css text/javascript </IfModule> <IfModule mod_deflate.c> <FilesMatch “\\.(js|css|html|xml)$”> SetOutputFilter DEFLATE </FilesMatch> </IfModule> <IfModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </IfModule> <IfModule mod_expires.c> ExpiresActive On ExpiresByType text/html "access plus 500 seconds" ExpiresByType text/xml "access plus 3600 seconds" ExpiresByType image/gif "access plus 14 days" ExpiresByType image/ico "access plus 14 days" ExpiresByType image/jpeg "access plus 14 days" ExpiresByType image/jpg "access plus 14 days" ExpiresByType image/png "access plus 14 days" ExpiresByType text/css "access plus 14 days" ExpiresByType text/javascript "access plus 14 days" ExpiresByType application/x-javascript "access plus 14 days" ExpiresByType application/javascript "access plus 14 days" ExpiresByType application/x-shockwave-flash "access plus 1000 seconds" ExpiresByType application/xhtml+xml "access plus 6000 seconds" </IfModule> <IfModule mod_headers.c> <FilesMatch "\\.(ico|jpeg|jpg|png|gif|swf)$"> Header set Cache-Control "max-age=2629743, public" </FilesMatch> <FilesMatch "\\.(css)$"> Header set Cache-Control "max-age=2592000, public" </FilesMatch> <FilesMatch "\\.(js)$"> Header set Cache-Control "max-age=216000, private" </FilesMatch> #<FilesMatch "\\.(xhtml|html|htm|php)$"> #Header set Cache-Control "max-age=600, private, must-revalidate" #</FilesMatch> </IfModule> AddType application/x-httpd-php .xml / try阻止围绕 catch声明 - 违反{ {3}}

但是:您仍然可以使用yield return / try - 您只需要在布局中保持创造性。例如:

catch

其他想法:

  • foreach (var item in task.Result) { IEnumerable<FileItem> subItems = null; FileItem subItem = null; try { FileItem retFile = item.ToFileItem(); logger.Info("Going Into " + retFile.Path); if (item.IsFolder == true) { subItems = FilesToDownload(retFile); } else { subItem = retFile; } } catch { /* your code here */ } if (subItem != null) yield return subItem; if (subItems != null) { foreach (var x in subItems) yield return x; } } Stack<T>可能比深度递归更合适
  • Queue<T>不好,可能会使您的代码陷入僵局;不幸的是,目前没有“async enumerables”的好模式