命名空间不能直接包含成员… +类型或命名空间定义,或文件结束预期错误

我正在尝试为Windows Phone编译Sync Framework 4.0的示例代码,但是我在几个文件中遇到了错误。 其中一个文件是:

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

有两个错误:

  • 对于第一个括号,命名空间不能直接包含诸如字段或方法之类的成员
  • 类型或命名空间定义,或期望的文件结束 – 用于最后一个括号

我已经通过谷歌搜索了这两个错误,并且我已经找到了很多这样的错误的答案 – 但是没有一个可以适用于我的情况(afaik没有丢失的括号)。

您收到此错误,因为您既没有定义SERVER也没有定义CLIENT 条件符号 。 在预处理阶段消除#if … #endif指令中的文本后,编译器只会看到以下代码:

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

这是无效的C#代码(因为在打开大括号之前缺少“namespace xyz”)。

在Visual Studio中,转到项目属性,然后在页面上构建设置条件编译符号到SERVER或CLIENT(名称区分大小写)。

我收到此错误是因为我的.TT文件中有UNIX样式换行符,大概是因为换行符是由git转换的。 将.tt文件复制到文本编辑器中,以PC格式保存,然后复制回Visual Studio解决了我的问题。