如何在C#中使指针通用?

时间:2013-02-19 08:55:24

标签: c#

我有WriteableBitmap,我使用不安全的方法绘制像素。基本部分看起来像这样:

private unsafe void DrawBitmap(WriteableBitmap bitmap, byte[] pixels)
{
         // Boilerplate omitted... 

        fixed (byte* pPixels = pixels)
        {
            for (int y = 0; y < height; y++)
            {
                var row = pPixels + (y * width);
                for (int x = 0; x < width; x++)
                {
                    *(row + x) = color[y + height * x];
                }
            }
        }

        bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width*pf.BitsPerPixel/8, 0);
}

但是,不确定用户想要绘制的是byte类型,并且将此方法设为通用(即DrawBitmap<T>)是有意义的,但我怎样才能使类型为pPixels的指针T*

是否还有其他一些技巧可以让我DrawBitmap通用?

1 个答案:

答案 0 :(得分:3)

考虑使用overloads

public void MethodA(byte[] pPixels) {}
public void MethodA(int[] pPixels {}
//etc...