Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "preprocessor" symbol corresponding to the /unsafe flag?

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.

like image 891
Eamon Nerbonne Avatar asked Feb 27 '09 23:02

Eamon Nerbonne


1 Answers

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.

like image 56
chuckj Avatar answered Nov 13 '22 13:11

chuckj