是否有一个“预处理器”? 与 /unsafe 标志对应的符号?

发布于 2024-07-14 20:27:16 字数 556 浏览 9 评论 0原文

我正在处理 C# 中的 WriteableBitmap。 我当前正在使用不安全的代码块通过 WriteableBitmap.BackBuffer 直接访问像素。 但是,我不想依赖 /unsafe 选项,因此我正在考虑使用 WriteableBitmap.WritePixels 代替。

是否有某种方法可以在“不安全”版本中进行条件编译,以便在使用 /unsafe 编译选项时可以使用它,而不需要手动集成到我的项目文件中?

简而言之,我正在寻找类似的东西:

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

运行时检测也很好; 但这意味着我总是需要使用 /unsafe 进行编译,这意味着库代码需要更新项目文件,这不太方便。

基本上,我想在重要的时候保留快速版本,但有一个无论如何都能工作的合理版本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲜血染红嫁衣 2024-07-21 20:27:16

我建议您使用配置管理器创建一个或多个新配置,例如“不安全调试”和“不安全发布”,这些配置具有现有选项并选中“不安全”并添加“不安全”的条件符号。 您可以使用不安全配置,而不是切换不安全选项。

您还可以让配置更改不安全程序集的输出名称,这样您就会有两个程序集,例如 Bitmaps.dll 和 Bitmaps.Unsafe.dll,并且程序集的客户端可以通过指定哪个程序集来决定哪个程序集最适合其需求。参考。

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.

一身骄傲 2024-07-21 20:27:16

我建议您使用 /unsafe /define:SAFE 进行编译。 也许还有另一种我不知道的方法。

I'd suggest that you compile with /unsafe /define:SAFE. Perhaps there is another way I'm not aware of.

Smile简单爱 2024-07-21 20:27:16

我想你可以尝试这个代码

#define UNSAFE
#undef UNSAFE // comment this line out if you want to compile the save version

然后你可以使用你在例子中给出的代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文