预处理器中的 C# 宏定义
C# 是否能够像 C 编程语言中使用预处理器语句那样定义宏? 我想简化某些重复语句的常规输入,如下所示:
Console.WriteLine("foo");
Is C# able to define macros as is done in the C programming language with pre-processor statements? I would like to simplify regular typing of certain repeating statements such as the following:
Console.WriteLine("foo");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不,C# 不支持像 C 一样的预处理器宏。另一方面,Visual Studio 有 片段。 Visual Studio 的代码片段是 IDE 的一项功能,在编辑器中展开,而不是在预处理器编译时在代码中替换。
No, C# does not support preprocessor macros like C. Visual Studio on the other hand has snippets. Visual Studio's snippets are a feature of the IDE and are expanded in the editor rather than replaced in the code on compilation by a preprocessor.
您可以使用 C 预处理器(如 mcpp)并将其装配到 .csproj 文件中。 然后,您将源文件上的“构建操作”从“编译”更改为“预处理”或任何您所称的名称。
只需将 BeforBuild 添加到您的 .csproj 中,如下所示:
您可能必须在至少一个文件上手动将“编译”更改为“预处理”(在文本编辑器中),然后“预处理”选项应该可供在 Visual Studio 中选择。
我知道宏被严重过度使用和误用,但完全删除它们即使不是更糟,也同样糟糕。 宏使用的一个经典示例是NotifyPropertyChanged。 每个必须手动重写此代码数千次的程序员都知道没有宏是多么痛苦。
You can use a C preprocessor (like mcpp) and rig it into your .csproj file. Then you chnage "build action" on your source file from Compile to Preprocess or whatever you call it.
Just add BeforBuild to your .csproj like this:
You may have to manually change Compile to Preprocess on at least one file (in a text editor) - then the "Preprocess" option should be available for selection in Visual Studio.
I know that macros are heavily overused and misused but removing them completely is equally bad if not worse. A classic example of macro usage would be NotifyPropertyChanged. Every programmer who had to rewrite this code by hand thousands of times knows how painful it is without macros.
我用它来避免
Console.WriteLine(...)
:然后您可以使用以下内容:
它简洁且有点时髦。
I use this to avoid
Console.WriteLine(...)
:and then you can use the following:
it's concise and kindof funky.
虽然您无法编写宏,但在简化示例之类的事情时,C# 6.0 现在提供静态使用。 以下是 Martin Pernica 在 他的 Medium 文章中给出的示例:
While you can't write macros, when it comes to simplifying things like your example, C# 6.0 now offers static usings. Here's the example Martin Pernica gave on his Medium article:
C# 中没有与 C 风格宏直接等效的东西,但
内联
静态方法 - 带或不带#if
/#elseif
/< code>#else pragmas - 是您可以获得的最接近的:这作为宏可以完美工作,但有一个小缺点:标记为
inline
d 的方法将被复制到反射部分像任何其他“正常”方法一样进行装配。There is no direct equivalent to C-style macros in C#, but
inline
d static methods - with or without#if
/#elseif
/#else
pragmas - is the closest you can get:This works perfectly as a macro, but comes with a little drawback: Methods marked as
inline
d will be copied to the reflection part of your assembly like any other "normal" method.幸运的是,C# 没有 C/C++ 风格的预处理器 - 仅支持条件编译和编译指示(可能还有其他我不记得的东西)。 不幸的是,C# 没有元编程功能(这可能实际上在某种程度上与您的问题相关)。
Luckily, C# has no C/C++-style preprocessor - only conditional compilation and pragmas (and possibly something else I cannot recall) are supported. Unfortunatelly, C# has no metaprogramming capabilities (this may actually relate to your question to some extent).
我建议你编写扩展,如下所示。
希望这会有所帮助(而且还不算太晚:))
I would suggest you to write extension, something like below.
Hope this helps (and not too late :) )
将 C 宏转换为类中的 C# 静态方法。
Turn the C Macro into a C# static method in a class.
使用 lambda
Use lambdas