调用与创建类不同的泛型类型的构造函数

发布于 2024-12-04 21:35:57 字数 1913 浏览 0 评论 0原文

我有这个问题,我希望这是一个简单的解决方案。我有以下课程(摘录),我想避免必须制作 public myImage(myImageimage), public myImage(myImageimage)public myImage(myImageimage) 以及可能的其他变体(如 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 创建方法,还是可以用 1 个方法完成这一切?

I have this problem which I hope is a simple solution to. I have the following class (extract) and I'd like to avoid having to make public myImage(myImage<int> image), public myImage(myImage<float> image), public myImage(myImage<double> image) and possible other variants as well (like 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)); }
    }
}

I'd use it like this:

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

So, do I have to create methods for byte, int, float, double or can 1 method do it all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2024-12-11 21:35:57

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

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);

You cannot use a generic argument in the contructor, but you could define a static method like this:

class myImage<T> where T : struct {

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

and then call it like

myImage<double> img2 = myImage<double>.FromImage<byte>(img);
划一舟意中人 2024-12-11 21:35:57

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

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

Your constructor cannot take an additional generic type from the class itself, but you can create some static factory methods, which can take additional types:

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));
        }
    }
}

Which could then be called like:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文