public class ImgBuffer<T>
{
public T[] buf;
public int width;
public int height;
public ImgBuffer () {}
public ImgBuffer (int w, int h)
{
buf = new T[w*h];
width = w;
height = h;
}
public void Mirror()
{
ImageTools.Mirror (ref buf, width, height);
}
}
ImageTools 类在第一个参数上为 byte[]、short[] 和 Color32[] 定义了 Mirror。特别是:
public void Mirror(ref Color32[] buf, int width, int height) { ....
但是我得到这个错误:
错误 CS1502:`ImageTools.Mirror(ref Color32[], int, int)' 的最佳重载方法匹配有一些无效参数
我做错了什么?
请您参考如下方法:
您似乎希望 C# 泛型类似于模板。事实并非如此。根据您的描述,似乎有一个 ImageTools 类看起来像这样
public class ImageTools
{
public static void Mirror(ref byte[] buf, int width, int height) { }
public static void Mirror(ref Color32[] buf, int width, int height) { }
public static void Mirror(ref short[] buf, int width, int height) { }
}
你有一个 ImgBuffer 类,看起来像这样(缩写)
public class ImgBuffer<T>
{
public T[] buf;
public int width;
public int height;
public void Mirror()
{
ImageTools.Mirror(ref buf, width, height);
}
}
编译器无法在 ImgBuffer.Mirror 中验证对 ImageTools.Mirror 的调用是否合法。编译器对 ref buf 的了解只是它是 T[] 类型。 T 可以是任何东西。它可以是 string、int、DateTime、Foo 等。编译器无法验证参数是否正确,因此您的代码是非法的。
这 是合法的,但我感觉您对 3 种所需类型的 Mirror 方法有特定的实现,因此它可能不可行。
public class ImageTools<T>
{
public static void Mirror(ref T[] buf, int width, int height) { }
}
