首页 > 解决方案 > Method Produces --> 扩展方法必须定义在非泛型静态类中

问题描述

每当我输入这个方法

  private Stream createstream(this System.Drawing.Image image, ImageFormat format)
        {
            var stream = new System.IO.MemoryStream();
            image.Save(stream, format);
            stream.Position = 0;
            return stream;
        }

我不断得到

扩展方法必须在非泛型静态类中定义

据我所知,我没有扩展任何东西。请指教

标签: c#.netextension-methods

解决方案


编译器认为您正在尝试编写扩展方法。扩展方法:

  1. 是静态方法
  2. 第一个参数this在它的开头
  3. 在非泛型静态类中定义

编译器this在您的 中看到this System.Drawing.Image image,认为您正在尝试编写扩展方法,并抱怨您没有遇到第 3 点。

如果您不打算编写扩展方法,请删除thisfrom this System.Drawing.Image image


推荐阅读