我如何通知我的接口的实现者“路径”是 参数代表一个文件夹?
我将在我的应用程序中定义一个接口,插件编写者可以实现该接口以提供用户定义的“导出”功能。 它看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为您没有实现该解决方案,所以我同意您使用 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.
使用 XML 注释,它们将显示在 Visual Studio 的 Intellisense 弹出窗口中。
Use XML comments, they will show up in Visual Studio's Intellisense popup.
更明确地命名您的变量。 它仅仅是一条路吗? 你说不,它不是,但你仍然给它留下一个通用名称。 将其命名为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.