在 C/C++ 中,您可以 #include 包含枚举定义的 .cs 文件。谨慎使用预处理器指令会处理 C# 和 C 之间的语法差异。
例子:
#if CSharp
namespace MyNamespace.SharedEnumerations
{
public
#endif
enum MyFirstEnumeration
{
Autodetect = -1,
Windows2000,
WindowsXP,
WindowsVista,
OSX,
Linux,
// Count must be last entry - is used to determine number of items in the enum
Count
};
#if CSharp
public
#endif
enum MessageLevel
{
None, // Message is ignored
InfoMessage, // Message is written to info port.
InfoWarning, // Message is written to info port and warning is issued
Popup // User is alerted to the message
};
#if CSharp
public delegate void MessageEventHandler(MessageLevel level, string message);
}
#endif
在您的 C# 项目中,设置条件编译符号“CSharp”,确保 C/C++ 构建环境中不存在此类预处理器定义。
请注意,这只会确保两个部分在构建时同步。如果您混合和匹配来自不同构建的二进制文件,则保证失败。