将VB.NET预处理程序指令转换为C#预处理程序指令

编程入门 行业动态 更新时间:2024-10-21 13:37:17
本文介绍了将VB.NET预处理程序指令转换为C#预处理程序指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将一些VB.NET代码移植到C#. 这是VB.NET

I am porting some VB.NET code to C#. Here is the VB.NET

#If _MyType = "WindowsForms" Then 'Some things to do ... #End If

根据我的阅读,C#将只处理布尔值#IF语句 显然是因为编译器不喜欢这样:

From what I have read C# will only deal with boolean #IF statements Apparently it is because the compiler does not like this:

#if _MyType = "WindowsForms" //Some things to do ... #endif

如何获得相同的结果?

How can I achieve the same results?

推荐答案

C#等效项是定义 WindowsForms ,然后执行以下操作: The C# equivalent would be to define WindowsForms and then to do this: #if WindowsForms // do stuff here #endif

您可以通过#define或通过项目设置来定义符号. 在代码中执行此操作的示例:

You can define a symbol either via #define or via the project settings. Example of doing it in code:

#define WindowsForms

~~~~~~~~~~ #define必须位于源文件中的任何其他标记之前.示例:

~~~~~~~~~~ The #define has to come before any other tokens in the source file. Example:

#define WindowsForms using System; namespace Nothing { class Program { static void Main(string[] args) { #if WindowsForms Console.WriteLine("WindowsForms defined"); #else Console.WriteLine("WindowsForms not defined"); #endif } } }

基本上,#if检查检查是否已定义符号.它无法检查值.它只检查定义.

Basically the #if check checks to see if the symbol is defined. It cannot check for a value. It only checks for definition.

有关预处理程序指令的详细信息在这里: msdn.microsoft/zh-CN/library/ed8yd1ha(v = VS.80).aspx [ ^ ] 如果您在我发布的网站上仔细阅读,则会发现此页面为您提供了对所有可用运营商的采访: msdn.microsoft/zh-CN/library/4y6tbswk(v = vs.71).aspx [ ^ ] 那应该解决您的问题. ("=="代表平等,而不仅仅是"=") 干杯! The details about preprocessor directives are here: msdn.microsoft/en-us/library/ed8yd1ha(v=VS.80).aspx[^] If you read carefully on the site I posted you''ll find that this page gives you an interview of all available operators: msdn.microsoft/en-us/library/4y6tbswk(v=vs.71).aspx[^] That should fix your problems. ("==" for equality not just "=") Cheers!

更多推荐

将VB.NET预处理程序指令转换为C#预处理程序指令

本文发布于:2023-11-14 00:38:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   程序   转换为   VB   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!