Socket通信中,客户端是C++写的,将结构体转出字符的形式,传过来。有字符串字段,但是长度是不定的。
服务端是C#,接收结构体
public struct PROTOCOL_SEND_CHAT_MESS
{
public UInt32 protocolId;
public UInt32 nextTest;
public bool isTest;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 28)]
public string mess;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 28)]
public string mess2;
}
protected static T Bytes2Struct<T>(byte[] bytes,T t)
{
IntPtr arrPtr = Marshal.UnsafeAddrOfPinnedArrayElement(bytes, 0);
return (T)Marshal.PtrToStructure(arrPtr, typeof(T));
}
现在SizeConst 是写死的,能不能动态修改这个SizeConst