调用与正在创建的类具有不同泛型类型的构造函数

本文关键字:构造函数 泛型类型 创建 调用 | 更新日期: 2023-09-27 18:08:46

我有这个问题,我希望是一个简单的解决方案。我有以下类(提取),我想避免不得不使public myImage(myImage<int> image), public myImage(myImage<float> image), public myImage(myImage<double> image)和可能的其他变体以及(如bool)。

class myImage<T> where T : struct
{
    private int width;
    private int height;
    private T[] img;
    // constructor: allocate new image
    public myImage(int Width, int Height)
    {
        img = new T[Width*Height];
        width = Width;
        height = Height;
    }
    public myImage(myImage<byte> image)
    {
        // allocate space for new
        width = image.Width;
        height = image.Height;
        img = new T[width * height];
        if (typeof(T) == typeof(byte))
        {
            // in and out = same type? use block copy
            Buffer.BlockCopy(image.Image, 0, img, 0, width * height * Marshal.SizeOf(typeof(T)));
        }
        else
        {
            // else copy image element by element
            for (int counter = 0; counter < width * height; counter++)
                img[counter] = (T)Convert.ChangeType(image[counter], typeof(T));
        }
    }
    public int Width
    { 
        get { return width; } 
    }
    public int Height
    {
        get { return height; }
    }
    public T[] Image
    {
        get { return img; }
    }
    public Object this[int index]
    {
        get { return img[index]; }
        set { img[index] = (T)Convert.ChangeType(value, typeof(T)); }
    }
}

我会这样使用:

myImage<byte> img = new myImage<byte>(100,200); // create byte image
myImage<double> img2 = new myImage<double>(img); // create double-img2 from byte-img
myImage<int> img3 = new myImage<int>(img2); // or int-img3 from double-img2

那么,我必须为byte, int, float, double创建方法吗?还是一个方法可以完成所有?

调用与正在创建的类具有不同泛型类型的构造函数

不能在构造函数中使用泛型参数,但可以这样定义一个静态方法:

class myImage<T> where T : struct {
    public static myImage<T> FromImage<X>(myImage<X> image) where X : struct {
        // create the object and return it...
    }
}

然后命名为

myImage<double> img2 = myImage<double>.FromImage<byte>(img);

构造函数不能从类本身获取额外的泛型类型,但是您可以创建一些静态工厂方法,这些方法可以获取额外的类型:

class myImage<T> where T : struct
{
    // ...
    public static myImage<T> CreateFrom<TOther>(myImage<TOther> image) where TOther : struct
    {
        // allocate space for new
        width = image.Width;
        height = image.Height;
        img = new T[width * height];
        if (typeof(T) == typeof(byte))
        {
            // in and out = same type? use block copy
            Buffer.BlockCopy(image.Image, 0, img, 0, width * height * Marshal.SizeOf(typeof(T)));
        }
        else
        {
            // else copy image element by element
            for (int counter = 0; counter < width * height; counter++)
                img[counter] = (T)Convert.ChangeType(image[counter], typeof(T));
        }
    }
}

可以这样调用:

myImage<double> img2 = myImage<double>.CreateFrom(img); // create double-img2 from byte-img
myImage<int> img3 = myImage<int>.CreateFrom(img2); // or int-img3 from double-img2