问题
I'm trying to compile the sample code for Sync Framework 4.0 for Windows Phone, however I've encountered an error in few files. One of those files is:
#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; }
}
}
There are two errors:
- A namespace cannot directly contain members such as fields or methods -- for the first parenthesis
- Type or namespace definition, or end-of-file expected -- for the last parenthesis
I've searched through the google for both of those errors and I've found lots of answers for such errors - however none of those can be applied to my case (afaik there are no missing parentheses).
回答1:
You get this error because you have neither SERVER nor CLIENT conditional symbol defined. After preprocessing phase eliminates text in #if...#endif directives, compiler sees only this code:
{
/// <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; }
}
}
which is not valid C# code (because "namespace xyz" is missing before opening curly brace).
In Visual Studio go to project properties and on page Build set Conditional compilation symbols to SERVER or CLIENT (names are case sensitive).
回答2:
I was getting this error because my .TT file had UNIX style line breaks in it, presumably because the line breaks were converted by git. Copying the .tt file into a text editor, saving it in PC format, and then copying back into Visual Studio solved the problem for me.
来源:https://stackoverflow.com/questions/10678672/a-namespace-cannot-directly-contain-members-type-or-namespace-definition-o