vb到预处理器的c#转换

时间:2015-10-20 14:22:53

标签: c#

任何人都可以帮助进行此转换,我在使用某些转换工具后对其进行了修改,但它无效。

    Public Sub New()

#If CONFIG = "Debug" Or CONFIG = "Release" Then
        strDBCNDN = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
        strDBCMDR = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
        strDBCQueueReader = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
#ElseIf CONFIG = "QA" Then
        strDBCNDN = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
        strDBCMDR = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
        strDBCQueueReader = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
#ElseIf CONFIG = "QA2" Then
        strDBCNDN = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
        strDBCMDR = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
        strDBCQueueReader = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
#ElseIf CONFIG = "Prod" Then
        strDBCNDN = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
        strDBCMDR = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
        strDBCQueueReader = System.Configuration.ConfigurationManager.ConnectionStrings("").ToString()
#End If
        isConnectionIssue = False

        Try
/*Here some code to fetch data from data base */
        Catch ex As Exception
            Throw New Exception(ex.ToString())
        End Try
    End Sub

在C#中转换如下所示也使用了define来定义页面顶部的已使用变量

public SameMethod()
    {


          #if CONFIG== Debug || CONFIG ==Release
                         {
                            strDBCNDN = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                            strDBCMDR = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                            strDBCQueueReader = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                         }
            #elif CONFIG==QA
                        {
                            strDBCNDN = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                            strDBCMDR = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                            strDBCQueueReader = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                        }

            #elif CONFIG== QA2
                        {
                            strDBCNDN = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                            strDBCMDR = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                            strDBCQueueReader = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                        }

            #elif CONFIG==Prod
                        {
                            strDBCNDN = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                            strDBCMDR = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                            strDBCQueueReader = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
                        }
       #endif
        isConnectionIssue = false;

        try
        {
            strAppName = System.Configuration.ConfigurationManager.AppSettings[""].ToString();

           /*some code for database*/
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
    }

1 个答案:

答案 0 :(得分:0)

根据此MSDN文章:https://msdn.microsoft.com/en-us/library/4y6tbswk.aspx - 您无法进行CONFIG == Debug等比较。这是因为C##IF预处理器只能检查是否定义了符号。这些符号只能是真或假。

您可以考虑将这些替换为#IF DEBUG#ELIF QA,因为这样您就可以为每个构建配置定义DEBUGQA符号。