Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a pointer generic in C#?

Tags:

c#

I have a WriteableBitmap which I use an unsafe method to plot pixels. The essential part looks like this:

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

However, it is not certain that what the user wants to plot is of the type byte, and it would make sense to make this method generic (i.e. DrawBitmap<T>) but how can I make the pointer pPixels of type T*?

Is there some other trick that will make my DrawBitmap generic?

like image 485
kasperhj Avatar asked Oct 22 '22 17:10

kasperhj


1 Answers

Consider using overloads.

public void MethodA(byte[] pPixels) {}
public void MethodA(int[] pPixels {}
//etc...
like image 118
LukeHennerley Avatar answered Oct 31 '22 16:10

LukeHennerley