我正在将一些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#预处理程序指令
发布评论