C ++代码是:
DLL_API DWORD WINAPI ExecuteCommand( LPCSTR, CONST COMMAND, CONST DWORD, LPREPLY);
typedef struct
{
REPLY_TYPE replyType;
union
{
POSITIVE_REPLY positiveReply;
NEGATIVE_REPLY negativeReply;
}
message;
}
REPLY;
我的C#代码是:
public struct Reply
{
public ReplyType ReplyType;
public PositiveReply PositiveReply;
public NegativeReply NegativeReply;
}
[DllImport(@"my.dll")]
public static extern int ExecuteCommand(string port, Command command, int timeout, ref Reply reply);
如何正确地将联合转移到C#代码?当我调用ExecuteCommand时,我会收到应该包含正面或负面回复的回复(例如C ++方法)。
答案 0 :(得分:0)
您无法在C#中复制C union。你应该这样做(根据你的真实代码更改类型名称和常量):
public struct Reply
{
public int rt;
public object o;
public int? pr { get { return rt == 1 ? (int?)o : null; } }
public int? nr { get { return rt == 0 ? (int?)o : null; } }
}