1

バイナリファイルがあります。1 つのデータは、8、16、32、64 ビットの符号付きおよび符号なし int にすることができます。テンプレート関数を作成しようとしていますが、これまでに行ったことは次のとおりです。

    public T[] GetLayerBytes<T>(int LayerNum)
    {
        int typeSize = Marshal.SizeOf(typeof(T));

        int layerPixelCount = typeSize * bitmapWidth * bitmapHeigth;

        string recFileName = "my.rec";

        using (FileStream fs = File.OpenRead(recFileName))
        using (BinaryReader br = new BinaryReader(fs))
        {
            fs.Seek(layerPixelCount * LayerNum, SeekOrigin.Begin);
            T[] b = new T[layerPixelCount];

            //fs.Read(b, 0, layerPixelCount); this doesn't work
            //br.Read(b, 0, layerPixelCount); this doesn't work too
            return b;
        }
    }

C++ では、これにCFile::Readを使用します。

さまざまなタイプのスイッチ/ケースなしで試したのと同様に、bytes/int16/uint16 などを読み取る方法はありTますか?

すべての可能なエレガントなソリューションを事前に感謝します。

4

1 に答える 1

1

あなたのコメントに基づいて、BinaryFormatter.Deserializeメソッドを使用することをお勧めします。

BinaryFormatter には、必要なオブジェクトのタイプを選択するために使用できるBinderプロパティがあります。

さらに、シリアル化されたデータを必要に応じて変換するために、 SurrogateSelectorを使用する必要があると思います...

于 2013-01-28T14:58:34.847 に答える