命名空间不能直接包含成员.+类型或命名空间定义,或文件结尾预期错误 [英] A namespace cannot directly contain members... + Type or namespace definition, or end-of-file expected errors

查看:42
本文介绍了命名空间不能直接包含成员.+类型或命名空间定义,或文件结尾预期错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译Windows Phone同步框架4.0的示例代码,但在几个文件中遇到错误。其中一个文件是:

#if SERVER
namespace Microsoft.Synchronization.Services
#elif CLIENT
namespace Microsoft.Synchronization.ClientServices
#endif
{
    /// <summary>
    /// Represents the base interface that all offline cacheable object should derive from.
    /// </summary>
    public interface IOfflineEntity
    {
        /// <summary>
        /// Represents the sync and OData metadata used for the entity
        /// </summary>
        OfflineEntityMetadata ServiceMetadata { get; set; }
    }
}

有两个错误:

  • 命名空间不能直接包含字段或方法等成员--第一个括号
  • 类型或命名空间定义,或文件结尾--用于最后一个括号

我已经在Google上搜索了这两个错误,找到了很多此类错误的答案--但是这些答案都不适用于我的案例(AFAIK没有漏掉的括号)。

推荐答案

您会收到此错误,因为您既没有定义服务器,也没有定义客户端conditional symbol。预处理阶段消除#if.#endif指令中的文本后,编译器仅看到以下代码:

{
    /// <summary>
    /// Represents the base interface that all offline cacheable object should derive from.
    /// </summary>
    public interface IOfflineEntity
    {
        /// <summary>
        /// Represents the sync and OData metadata used for the entity
        /// </summary>
        OfflineEntityMetadata ServiceMetadata { get; set; }
    }
}

这不是有效的C#代码(因为在打开大括号之前缺少"命名空间xyz")。

在Visual Studio中,转到"项目属性",并在"生成"页上将条件编译符号设置为服务器或客户端(名称区分大小写)。

这篇关于命名空间不能直接包含成员.+类型或命名空间定义,或文件结尾预期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆