A namespace cannot directly contain members... + Type or namespace definition, or end-of-file expected errors(命名空间不能直接包含成员.+类型或命名空间定义,或文件结尾预期错误)
问题描述
我正在尝试编译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中,转到"项目属性",并在"生成"页上将条件编译符号设置为服务器或客户端(名称区分大小写)。
这篇关于命名空间不能直接包含成员.+类型或命名空间定义,或文件结尾预期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:命名空间不能直接包含成员.+类型或命名空间定义,或文件结尾预期错误


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01