我需要将以下结构转换为字节数组:
[Serializable]
public struct newLeads
{
public string id;
public string first_name;
public string last_name;
}
我正在尝试使用以下代码转换为字节数组:
public class ConvertStruct
{
public static byte[] StructureToByteArray(object obj)
{
int Length = Marshal.SizeOf(obj);
byte[] bytearray = new byte[Length];
IntPtr ptr = Marshal.AllocHGlobal(Length);
Marshal.StructureToPtr(obj, ptr, false);
Marshal.Copy(ptr, bytearray, 0, Length);
Marshal.FreeHGlobal(ptr);
return bytearray;
}
}
我在网上收到异常:
IntPtr ptr = Marshal.AllocHGlobal(Length);
Exception: Attempt by security transparent method 'Classes.ConvertStruct.ConvertStruct.StructureToByteArray(System.Object)' to access security critical method 'System.Runtime.InteropServices.Marshal.AllocHGlobal(Int32)' failed."}
我的问题是?我该如何解决这个问题以避免异常并将我的简单结构转换为 byte[]
?
提前致谢!
更新:我在控制台应用程序中试过这个并且它有效。我是从 asp.net 页面代码隐藏中调用它的,所以这一定与它有关,但我不知道是什么!
最佳答案
- 检查结构的大小,不要装箱。
为字符串设置适当的编码(marshal)处理(取自 answer 的 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;
}
注意:我没有使用以 null 结尾的字符串,因为我不知道你最终会使用什么编码。
优化
相同的逻辑,但使用流实现(Union 没有改变)[已测试]。
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));
}
关于c# - 尝试将结构转换为 byte[] 时的方法访问预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10843295/