I’m creating a performance-critical application that implements image manipulation. I’m using some pixel pointers using my struct named Pixel to do some processing. I’ve got lots of code parts that iterate over the whole bitmap data, and for the sake of code reusability and modularity, I’m designing a method that will take an action and apply it to all pixels of the image (like a map function). However, when I write Action<Pixel*> Visual Studio complains about the code saying the type Pixel* may not be used as a type argument. The whole class is in an unsafe context and I’m using Pixel pointers everywhere, but I just can’t use a pixel pointer as an Action’s template class.
I can use Action<IntPtr> but I’ll need to convert it to appropriate pointers inside the method body in EVERY iteration, which would kill the whole idea of being “performance critical”.
It seems nothing is forcing you to use
Action<T>, so you can create your own delegate type. I didn’t figure out a way to do this in a generic way, but this works:Keep in mind that if this is really performance critical, invoking a delegate is slower than just calling a method directly. Maybe another way could be better, like duplicating code (if that would work in your case), or code generation, either at compile time or at runtime using Reflection.Emit or CodeDOM.