2011-09-15 7 views
4

私はこの問題を抱えています。私は以下のクラス(抽出物)を持っています。私はpublic myImage(myImage<int> image)public myImage(myImage<float> image)public myImage(myImage<double> image)などの可能性のあるバリエーション(ブールのようなもの)を作らなければならないようにしたいと思います。クラスとは異なるジェネリックタイプのコンストラクタが呼び出されています

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 

をだから、それはすべて私がバイト、int型、float型、ダブルまたは缶1の方法のためのメソッドを作成しなければならないのですか?

答えて

4

あなたは、コンストラクタで、一般的な引数を使用することはできませんが、このような静的なメソッド定義することができます。

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

このように呼び出されませんか? 'myImage img2 = myImage .FromImage (img);'これが私が試したものです。それは私のために十分です。ありがとうございました。 – galmok

+0

はい、あなたは正しいです、ごめんなさい: –

2

追加のジェネリックを取ることができないあなたのコンストラクタクラス自体から型を作成することもできますが、追加の型を取ることができる静的ファクトリメソッドを作成できます。

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 
+0

Paolo Tedescoと基本的に同じ返信ですが、もちろん私はクラスを手作業で割り当てる方法を少し変更しなければならなかった。しかし、それは動作します。 :) – galmok

+0

うん、私たちは同時に、私たちの反応に取り組んでいた...多くのことが起こる。 –

関連する問題