You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何无法在Form1中访问public类ResourceDownloader?

无法访问ResourceDownloader类的解决方法

以下是针对你在Form1中无法访问public修饰的ResourceDownloader类的排查步骤:

  • 添加命名空间引用
    ResourceDownloader类位于Download_Files命名空间下,需在Form1的代码文件顶部添加:

    using Download_Files;
    

    或者直接使用完整限定名称调用类:Download_Files.ResourceDownloader

  • 确认文件归属项目
    确保包含ResourceDownloader类的.cs文件已被添加到Form1所在的项目中。若未添加,右键项目 → 添加 → 现有项,选择该类文件。如果是多项目解决方案,需在Form1的项目中引用包含ResourceDownloader的项目。

  • 修复类自身编译错误
    检查ResourceDownloader类的代码是否存在编译问题,它需要补充以下命名空间引用(添加在类文件顶部):

    using System.Collections.Concurrent;
    using System.Diagnostics;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    

    只有类本身编译通过,才能被其他类正常识别。

  • 清理重建项目
    执行菜单栏的「生成 → 清理解决方案」,再执行「生成 → 重建解决方案」,清除编译缓存后重新尝试引用。


完整的ResourceDownloader类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading;

namespace Download_Files
{
    public class ResourceDownloader
    {

        private static readonly Lazy<HttpClient> client = new(() => {
            HttpClientHandler handler = CreateHandler(autoRedirect: true);

            var client = new HttpClient(handler, true) { Timeout = TimeSpan.FromSeconds(60) };
            client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");
            client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
            client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
            // 若每次下载不同集合的URL,保持为true;若频繁重复使用相同URL,设为false或移除该配置
            client.DefaultRequestHeaders.ConnectionClose = true;
            return client;
        }, true);

        private static HttpClientHandler CreateHandler(bool autoRedirect)
        {
            return new HttpClientHandler()
            {
                AllowAutoRedirect = autoRedirect,
                CookieContainer = new CookieContainer(),
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };
        }

        public record WebData(string Url, byte[]? Data, bool Completed = true, Exception? Ex = null);
        public record ProgressReport(WebData Site, int PercentageComplete);

        private static object syncObj = new object();
        private static ConcurrentBag<WebData> processed = default!;
        private static int progressCount = 0;
        private static int totalCount = 0;

        public static bool IsBusy { get; internal set; } = false;

        public static async Task<List<WebData>> Download(IProgress<ProgressReport> progress, IList<string> sites, CancellationTokenSource cts)
        {
            IsBusy = true;
            processed = new ConcurrentBag<WebData>();
            progressCount = 0;
            totalCount = sites.Count;

            try
            {
                ParallelOptions options = new()
                {
                    // 单站点下载时设置合适值避免被拉黑;多站点可适当提高
                    MaxDegreeOfParallelism = 8,
                    CancellationToken = cts.Token
                };

                await Parallel.ForEachAsync(sites, options, async (site, token) => {
                    try
                    {
                        var dataBytes = await client.Value.GetByteArrayAsync(site, token);
                        ReportProgress(progress, dataBytes, site, null);
                    }
                    catch (Exception ex)
                    {
                        ReportProgress(progress, null, site, ex);
                    }
                });
            }
            // 调试/日志用
            catch (TaskCanceledException) { Debug.Print("操作已取消"); }
            finally { IsBusy = false; }
            return processed.ToList();
        }

        private static void ReportProgress(IProgress<ProgressReport> progress, byte[]? data, string site, Exception? ex)
        {
            lock (syncObj)
            {
                progressCount += 1;
                var percentage = progressCount * 100 / totalCount;
                WebData webData = new(site, data, ex is null, ex);
                processed.Add(webData);
                progress.Report(new ProgressReport(webData, percentage));
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Daniel Lip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 18:55:30