- 检查结构的大小,不要装箱。
为字符串设置一个适当的封送处理(从goric的答案中获取),除此之外,您将得到的是字节数组中字符串的内存地址(不是一件好事)。
[Serializable]
public struct newLeads
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5000)]
public string id;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5000)]
public string first_name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5000)]
public string last_name;
}
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
public static byte[] ToByteArray(newLeads value)
{
int length = Marshal.SizeOf(typeof(newLeads));
var result = new byte[length];
IntPtr sourcePtr = Marshal.AllocHGlobal(length);
Marshal.StructureToPtr(value, sourcePtr, false);
Marshal.Copy(sourcePtr, result, 0, length);
Marshal.FreeHGlobal(sourcePtr);
return result;
}
在您的评论中,您说此代码会更快失败。好吧,它使用安全权限需求(如 .NET 4 推荐的那样),每次调用该方法时都会检查特定权限。您可以尝试在没有它的情况下执行它,而预期的结果就是您一开始得到的结果。
真正的答案
您必须在受限环境中运行,可能是某种沙箱或不支持指针的平台。在这种情况下,我们可能需要通过其他方式进行转换。
你说的是 ASP.NET?就是这样了。
为了在没有指针的情况下进行转换,请尝试以下技术:
//You will have to decide an encoding.
//If nto ASCII, try UTF8Encoding, UnicodeEncoding or (Hopefully not) UTF7Encoding
void Main()
{
Encoding encoding = new ASCIIEncoding();
newLeads target = GetNewLeads();
byte[] id = EncodeString(target.id, encoding);
byte[] first_name = EncodeString(target.first_name, encoding);
byte[] last_name = EncodeString(target.last_name, encoding);
}
byte[] EncodeString(string str, Encoding encoding)
{
byte[] data;
if (ReferenceEquals(str, null))
{
data = new byte[0];
}
else
{
data = encoding.GetBytes(str);
}
return data;
}
在这一点上,我需要更多地了解您的情况,以便为您提供更好的解决方案,特别是谁或什么将读取该字节数组?无论如何,您可以对字符串的长度进行编码,如下所示(为 null 保留 -1):
byte[] EncodeString(string str, Encoding encoding)
{
byte[] data;
byte[] data_length;
Union union = new Union();
if (ReferenceEquals(str, null))
{
data = new byte[0];
union.data = -1;
}
else
{
data = encoding.GetBytes(str);
union.data = str.Length;
}
data_length = new byte[]{union.a, union.b, union.c, union.c};
int length = data.Length;
byte[] result = new byte[4 + data.Length];
System.Buffer.BlockCopy(data_length, 0, result, 0, 4);
System.Buffer.BlockCopy(data, 0, result, 4, length);
return result;
}
//I hope endianess doesn't bite
[StructLayout(LayoutKind.Explicit)]
struct Union
{
[FieldOffset(0)]
public int data;
[FieldOffset(0)]
public byte a;
[FieldOffset(1)]
public byte b;
[FieldOffset(2)]
public byte c;
[FieldOffset(3)]
public byte d;
}
最后,我们需要加入这些数组。我知道这可以进一步优化......(使用 MemoryStream 和 StreamWriter 的一个好主意)无论如何,这是我的第一个实现 [测试]:
byte[] ToByteArray(newLeads value)
{
Encoding encoding = new ASCIIEncoding(); //Choose some encoding
byte[] id = EncodeString(value.id, encoding);
byte[] first_name = EncodeString(value.first_name, encoding);
byte[] last_name = EncodeString(value.last_name, encoding);
byte[] result = new byte[id.Length + first_name.Length + last_name.Length];
System.Buffer.BlockCopy(id, 0, result, 0, id.Length);
System.Buffer.BlockCopy
(
first_name,
0,
result,
id.Length,
first_name.Length
);
System.Buffer.BlockCopy
(
last_name,
0,
result,
id.Length + first_name.Length,
last_name.Length
);
return result;
}
byte[] EncodeString(string str, Encoding encoding)
{
byte[] data;
byte[] data_length;
Union union = new Union();
if (ReferenceEquals(str, null))
{
data = new byte[0];
union.data = -1;
}
else
{
data = encoding.GetBytes(str);
union.data = str.Length;
}
data_length = new byte[]{union.a, union.b, union.c, union.c};
int length = data.Length;
byte[] result = new byte[4 + data.Length];
System.Buffer.BlockCopy(data_length, 0, result, 0, 4);
System.Buffer.BlockCopy(data, 0, result, 4, length);
return result;
}
//I hope endianess doesn't bite
[StructLayout(LayoutKind.Explicit)]
struct Union
{
[FieldOffset(0)]
public int data;
[FieldOffset(0)]
public byte a;
[FieldOffset(1)]
public byte b;
[FieldOffset(2)]
public byte c;
[FieldOffset(3)]
public byte d;
}
注意:我没有使用以空字符结尾的字符串,因为我不知道你最终会使用什么编码。
优化
相同的逻辑,但用流实现(联合没有改变)[测试]。
byte[] ToByteArray(newLeads value)
{
Encoding encoding = new ASCIIEncoding(); //Choose some encoding
var stream = new MemoryStream();
EncodeString(value.id, stream, encoding);
EncodeString(value.first_name, stream, encoding);
EncodeString(value.last_name, stream, encoding);
int length = (int)stream.Length;
byte[] result = new byte[(int)stream.Length];
System.Buffer.BlockCopy(stream.GetBuffer(), 0, result, 0, length);
stream.Close();
return result;
}
void EncodeString(string str, Stream stream, Encoding encoding)
{
Union union = new Union();
if (ReferenceEquals(str, null))
{
union.data = -1;
stream.Write(new byte[]{union.a, union.b, union.c, union.c}, 0, 4);
}
else
{
union.data = str.Length;
stream.Write(new byte[]{union.a, union.b, union.c, union.c}, 0, 4);
var tmp = encoding.GetBytes(str);
stream.Write(tmp, 0, tmp.Length);
}
}
把琴弦拉回来
为了取回数据,我们首先读取字符串的长度(使用相同的 Union 类型):
var newLeads = GetNewLeads();
var z= ToByteArray(newLeads); //we have the byteArray in z
var data = new MemoryStream(z); //Create an stream for convenience
//Use the union to get the length
var union = new Union()
{
a = (byte)data.ReadByte(),
b = (byte)data.ReadByte(),
c = (byte)data.ReadByte(),
d = (byte)data.ReadByte()
};
Console.WriteLine(union.data); //the length of the first string
我们的下一步是读取那么多字符,为此我们将使用 StreamReader:
var newLeads = GetNewLeads();
var z = ToByteArray(newLeads);
var data = new MemoryStream(z);
var union = new Union()
{
a = (byte)data.ReadByte(),
b = (byte)data.ReadByte(),
c = (byte)data.ReadByte(),
d = (byte)data.ReadByte()
};
var encoding = new ASCIIEncoding();
string result = null;
if (union.data != -1)
{
char[] finalChars = new char[union.data];
var reader = new StreamReader(data, encoding);
reader.Read(finalChars, 0, union.data);
result = new string(finalChars);
}
Console.WriteLine(result);
有了这个,我们构建了一个解码字符串的方法:
string DecodeString(Stream data, Encoding encoding)
{
//TODO: You may want to validate that data and encoding are not null
//or make this private
var union = new Union()
{
a = (byte)data.ReadByte(),
b = (byte)data.ReadByte(),
c = (byte)data.ReadByte(),
d = (byte)data.ReadByte()
};
string result = null;
if (union.data != -1)
{
char[] finalChars = new char[union.data];
var reader = new StreamReader(data, encoding);
reader.Read(finalChars, 0, union.data);
result = new string(finalChars);
}
return result;
}
//Convenience method, not needed:
string DecodeString(byte[] data, Encoding encoding)
{
//TODO: You may want to validate that data and encoding are not null
//or make this private
return DecodeString(new MemoryStream(data), encoding);
}
最后是恢复 newLeads 的方法(再次使用相同的 Union 类型)[已测试]:
newLeads FromByteArray(byte[] data)
{
//TODO: Validate that data is not null
Encoding encoding = new ASCIIEncoding(); //Choose the same encoding
newLeads result = new newLeads();
var reader = new StreamReader(new MemoryStream(data), encoding);
result.id = DecodeString(reader);
result.first_name = DecodeString(reader);
result.last_name = DecodeString(reader);
reader.Close();
return result;
}
//Changed to reuse StreamReader...
//Because closing it will close the underlying stream
string DecodeString(StreamReader reader)
{
//TODO: You may want to validate that reader is not null
//or make this private
var data = reader.BaseStream;
var union = new Union()
{
a = (byte)data.ReadByte(),
b = (byte)data.ReadByte(),
c = (byte)data.ReadByte(),
d = (byte)data.ReadByte()
};
string result = null;
if (union.data != -1)
{
char[] finalChars = new char[union.data];
reader.Read(finalChars, 0, union.data);
result = new string(finalChars);
}
return result;
}
//Convenience method, not needed:
string DecodeString(byte[] data, Encoding encoding)
{
//TODO: You may want to validate that data and encoding are not null
//or make this private
return DecodeString(new StreamReader(new MemoryStream(data), encoding));
}