繁体   English   中英

C#预处理程序指令(#if和#endif)不起作用。 编译错误

[英]C# Preprocessor Directives (#if and #endif) not working. Compilation error

我正在尝试调试VS2010 4.0框架中的现有应用程序。 我收到此编译时错误:

“名称'b_resources'在当前上下文中不存在”。

我在下面的代码中看不到任何错误:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;

#if TR
    using b_resources = Common.Resources.TResources;
#endif

#if EN
    using b_resources = Common.Resources.EnResources;
#endif

namespace Common
{
    public static class BResources
    {

        public static string caption { get { return b_resources.ProductName; } }
        public static string company_name { get { return b_resources.CompanyName; } }
    }
}

TResourcesEnResources是资源文件(.resx)

我是否缺少一些与.Net相关的参考?

最明显的问题是:您在当前版本中定义了TREN编译符号吗? 如果不是,则两个#IF语句都将为false,并且将不包括using语句。 换句话说,如果不定义这些符号,则将编译以下代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;

// Note: missing using statements

namespace Common
{
    public static class BResources
    {

        public static string caption { get { return b_resources.ProductName; } }
        public static string company_name{ get { return b_resources.CompanyName; } }
    }
}

要检查或设置编译符号,请转到项目的属性。 然后,在“构建”选项卡上,您将看到一个名为“条件编译符号”的文本框。 检查符号是否在那里定义。 如果没有,请在其中添加其中之一。

如果您使用的是Visual Studio,实际上您可以立即查看是否是这种情况。 如果当前构建未定义符号,则#IF块中的代码将显示为灰色。 如果定义了符号,它将像普通代码一样显示jsut。

您没有默认情况,因此如果未定义TR或EN,则不会获得b_resources的定义。 您需要在其中包含其他一些情况,以便始终可以进行编译。 例如,如果您希望将EN资源设置为默认值:

#if TR
    using b_resources = Common.Resources.TResources;
#elif EN
    using b_resources = Common.Resources.EnResources;
#else
    using b_resources = Common.Resources.EnResources; // or whatever you want as default
#endif

如果未定义TR或EN,则最后一个将被包括在内。

为了编译代码,您需要在Project属性的Build设置中定义TR或EN。 当前都未定义,因此b_resources从未定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM