【问题标题】:How do I pass one method as a parameter to another method?如何将一种方法作为参数传递给另一种方法?
【发布时间】:2013-08-11 02:28:51
【问题描述】:

我有这个代码:

private void SearchForDoc()
        {
            try
            {
                outputtext = @"c:\temp\outputtxt";
                outputphotos = @"c:\temp\outputphotos";
                temptxt = @"c:\temp\txtfiles";
                tempphotos = @"c:\temp\photosfiles";
                if (!Directory.Exists(temptxt))
                {
                    Directory.CreateDirectory(temptxt);
                }
                if (!Directory.Exists(tempphotos))
                {
                    Directory.CreateDirectory(tempphotos);
                }
                if (!Directory.Exists(outputtext))
                {
                    Directory.CreateDirectory(outputtext);
                }
                if (!Directory.Exists(outputphotos))
                {
                    Directory.CreateDirectory(outputphotos);
                }
                t = Environment.GetEnvironmentVariable(Environment.GetFolderPath(Environment.SpecialFolder.Personal));

                ApplyAllFiles(t,ProcessFile(t);
                for (int i = 0; i < textfiles.Length; i++)
                {


                        FileInfo fi = new FileInfo((textfiles[i]));
                        DirectoryInfo d = new DirectoryInfo(temptxt);
                        long dirSize = DirSize(d);

                        if ((dirSize + fi.Length) <= 8388608)
                            fi.CopyTo(temptxt + "\\" + fi.Name, true);
                        else
                            break;

                }

然后在这之后我有两种方法:

static void ProcessFile(string path) {/* ... */}
        static void ApplyAllFiles(string folder, Action<string> fileAction)
        {
            foreach (string file in Directory.GetFiles(folder))
            {
                fileAction(file);
            }
            foreach (string subDir in Directory.GetDirectories(folder))
            {
                try
                {
                    ApplyAllFiles(subDir, fileAction);
                }
                catch
                {
                    // swallow, log, whatever
                }
            }
        }

在我的方法中使用这两种方法应该从文档目录及其所有子目录中获取所有文本文件。

按照我的方法:

ApplyAllFiles(t,ProcessFile(t);

但这是错误的使用方式。我该如何使用这些方法?

【问题讨论】:

    标签: c# winforms function delegates


    【解决方案1】:

    由于ProcessFile 方法已经与Action&lt;string&gt; 具有相同的签名,您只需指定方法名称即可:

    ApplyAllFiles(t, ProcessFile);
    

    【讨论】:

    • p.s.w.g 好的,现在我如何使用我的 FOR 循环,因为变量 textfiles 不再退出:for (int i = 0; i
    • @DanielVest textfiles 应该是在ApplyAllFiles 中处理的所有文件的列表吗?
    • p.s.w.g 我猜...textfiles is string[] 应该包含 ApplyAllFiles 中的所有文件,但只包含文本文件!
    • @DanielVest 你如何确定它是否是一个文本文件? ProcessFile 是这样做的吗?
    • @DanielVest 这应该对你有用,实际上ProcessFile使用的参数是t,可以在ApplyAllFiles的代码中看到,如果你还有什么问题,您应该问另一个,这正确回答了您询问如何将方法 ProcessFile 传递给方法 ApplyAllFiles 的问题。
    猜你喜欢
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 2014-02-24
    • 2020-12-07
    • 2018-12-24
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多