如何将扩展方法扩展到 System.IO.File

How to make extension method to System.IO.File

如果有办法,我想知道在c#中对System.IO.File进行扩展方法的方法。

我想做一个像下面这样的方法。

// 'CreateDirectoryAndFile is the method I made
System.IO.File.CreateDirectoryAndFile("path"); 

但是我在google上搜索过,找不到路

所以,我被迫尝试像下面那样做。

// I made a FileHelper class for this method.
FileHelper.CreateDirectoryAndFile("path");

没有这样的方法吗?

您不能为静态创建扩展方法 class。

但是,如果您需要 'extend' 很多功能,您可以为 System.IO.File 创建包装器。喜欢:

public class FileWrapper 
{
   public File CreteFile(string path)
   {
   }

   public File CreateDirectoryAndFile(string path)
   {
   }

   // etc
}