0

For such an extension:

public static class ImageExtensions
{
    public static Image LoadImage(string path)
    {
        using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
            return Image.FromStream(ms);
    }
}

How to make it possible to be called Image.LoadImage(path) instead of ImageExtensions.LoadImage(path)?

I can normally use other extension methods, where their first parameter is this Image img. Those work by using instance.Method().

ebvtrnog
  • 4,167
  • 4
  • 31
  • 59
  • similar question with some more detailed answers: http://stackoverflow.com/questions/249222/can-i-add-extension-methods-to-an-existing-static-class – Kevin Nacios Mar 07 '15 at 18:15

2 Answers2

2

It is impossible. You cannot write extension methods that act as static extensions, because extensions methods require an object instance.

user3711864
  • 349
  • 2
  • 10
0

Actually, a method with signature public static Image LoadImage(string path isn't even an extension. It is just a static method, therefore to write this as you would like it simply change the class name:

public static class Image
{
    public static Image LoadImage(string path)
    {
        using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
            return Image.FromStream(ms);
    }
}
THBBFT
  • 1,161
  • 1
  • 13
  • 29