代码之家  ›  专栏  ›  技术社区  ›  Matt Hamilton

如何通知接口的实现者“path”参数表示文件夹?

  •  0
  • Matt Hamilton  · 技术社区  · 17 年前

    我将在我的应用程序中定义一个接口,插件编写器可以实现该接口以提供用户定义的“导出”功能。它看起来像这样:

    public interface IFooExporter
    {
        void ExportFoo(Foo foo, string path);
    }
    

    强制路径是文件夹而不是文件名的最佳方式是什么?我现在最好的猜测是使用DirectoryInfo而不是字符串:

    public interface IFooExporter
    {
        void ExportFoo(Foo foo, DirectoryInfo folder);
    }
    

    这是一个好的解决方案,还是在传递DirectoryInfo实例时存在我不知道的陷阱?

    3 回复  |  直到 17 年前
        1
  •  1
  •   benPearce    17 年前

    因为您没有实现该解决方案,所以我同意您使用DirectoryInfo作为参数的解决方案。如果指定一个字符串,则无法停止传递任何字符串。

        2
  •  1
  •   TravisO    17 年前

    /// <summary>
    /// Type in the text you want to appear
    /// </summary>
    
        3
  •  1
  •   ChrisN    17 年前

    更明确地命名变量。这仅仅是一条路吗?你说不,不是,但你还是用一个通用的名字。将其命名为folderPath,将减少混淆,也不需要将其显式地传达给实现者。