我如何通知我的接口的实现者“路径”是 参数代表一个文件夹?

发布于 2024-07-09 15:11:09 字数 493 浏览 9 评论 0原文

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

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

但是,我需要让插件编写者知道(明确地,不仅仅是在文档中)“路径”代表一个文件夹,而不是文件名。 他们负责在导出过程中创建文件。

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

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

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

I am about to define an interface in my application that plug-in writers can implement to provide user-defined "export" capabilities. It'll look something like this:

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

However, I need to let the plug-in writers know (explicitly, not just in documentation) that "path" represents a folder, not a filename. It's their responsibility to create the files as part of the export process.

What's the best way to enforce that a path is a folder and not a filename? My best guess right now is to use DirectoryInfo instead of string:

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

Is that a good solution, or are there pitfalls I'm not aware of with passing DirectoryInfo instances around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2024-07-16 15:11:09

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

Because you are not implementing the solution, then I agree with you solution of using the DirectoryInfo as a parameter. If you specify a string then there is no way to stop any string being passed.

撩发小公举 2024-07-16 15:11:09

使用 XML 注释,它们将显示在 Visual Studio 的 Intellisense 弹出窗口中。

/// <summary>
/// Type in the text you want to appear
/// </summary>

Use XML comments, they will show up in Visual Studio's Intellisense popup.

/// <summary>
/// Type in the text you want to appear
/// </summary>
Spring初心 2024-07-16 15:11:09

更明确地命名您的变量。 它仅仅是一条路吗? 你说不,它不是,但你仍然给它留下一个通用名称。 将其命名为folderPath,将减少混乱,也无需向实施者明确传达这一点。

Name your variable more explicitly. Is it merely a path? You're saying no it is not, but you're still leaving it with a generic name. Name it folderPath and there will be less confusion and less need to explicitly communicate this to implementers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文