Reputation: 48096
I'm dealing with a WriteableBitmap
in C#. I'm currently using an unsafe code block to directly access the pixels via WriteableBitmap.BackBuffer
. I'd rather not depend on the /unsafe
option, however, so I'm considering using WriteableBitmap.WritePixels
instead.
Is there some way of conditionally compiling in the "unsafe" version such that it can be used when the /unsafe option for compilation was used, without requiring manual integration into my project file?
In short I'm looking for something along the lines of:
#if UNSAFE
//my unsafe version
#else
//the supposedly safe version goes here
#endif
Detection at runtime is nice too; but that means I always need to compile with /unsafe
, and that means the library code would require project file updates, which is less handy.
Basically, I want to keep the fast version for when it matters, but have a reasonable version that just works no matter what.
Upvotes: 10
Views: 1961
Reputation: 29575
I recommend you create one or more new configurations using the configuration manager, say "Unsafe Debug" and "Unsafe Release", that have the existing options plus check Unsafe and add a conditional symbol of UNSAFE. Instead of toggling the Unsafe options you would use the Unsafe configuration.
You could also have the configurations change the output name of the unsafe assembly so you would have two assemblies, say Bitmaps.dll and Bitmaps.Unsafe.dll, and a client of the assembly can decide which fits its needs best by specifying which assembly it references.
Upvotes: 6
Reputation: 37211
I think you might try this code
#define UNSAFE
#undef UNSAFE // comment this line out if you want to compile the save version
Then you can use the code like you gave in your example.
Upvotes: 0
Reputation: 41660
I'd suggest that you compile with /unsafe /define:SAFE
. Perhaps there is another way I'm not aware of.
Upvotes: 1