Is there a preprocessor character matching the / unsafe character?

I am dealing with WriteableBitmap in C #. I am currently using an insecure code block for direct access to pixels through WriteableBitmap.BackBuffer . However, I would prefer not to rely on the /unsafe option, so I am considering using WriteableBitmap.WritePixels instead.

Is there any way to conditionally compile in the "unsafe" version so that it can be used when the / unsafe option was used for compilation, without the need for manual integration into the project file?

In short, I'm looking for something like:

 #if UNSAFE //my unsafe version #else //the supposedly safe version goes here #endif 

Runtime detection is also good; but this means that I always need to compile with /unsafe , which means that the library code will require updates to the project files, which is less convenient.

Basically, I want to keep the fast version when it matters, but has a reasonable version that works no matter what.

+6
c # c-preprocessor wpf unsafe
source share
3 answers

I recommend that you create one or more new configurations using the configuration manager, say, “Unsafe debugging” and “Unsafe version”, which have existing options, as well as checking “Insecure” and adding a UNSAFE conditional character. Instead of switching the Unsafe options, you should use the Unsafe configuration.

You can also reconfigure the output name of the unsafe assembly so that you have two assemblies, for example Bitmaps.dll and Bitmaps.Unsafe.dll, and the assembly client can decide which one works best by specifying with which it refers.

+4
source share

I suggest you compile with /unsafe /define:SAFE . Perhaps there is another way that I do not know about.

+1
source share

I think you can 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 as you indicated in your example.

0
source share

All Articles