字典的序列化/反序列化将 dll 注入其他进程时在 C++/CLI 中需要
我需要在 C++/CLI 中序列化一个对象数组 array
,其中包含一个 intPtr
和一个字典 Dictionary
code> 其中 ObjectInfo
是一些自定义类。它可以正确序列化,但在反序列化时会出现以下错误:
“System.Runtime.Serialization..SerializationException:无法
加载类型 System.Collections.Generic.Dictionary'2[[System.IntPtr,mscorlib, 版本=4.0.0.0", 文化=中性,PublicKeyToken=............],[System.Collection.Generic.List'1[[SharedObjects.ObjectInfo,SharedObjects,版本......]] 需要反序列化。
我将此 array
作为参数传递给 Type->InvokeMember
,并且这些参数需要在传递给 InvokeMember 之前进行反序列化
。
但在反序列化过程中失败。
序列化代码:
void Serialize(Object^ obj)
{
if (obj == nullptr)
{
_length = 0;
return;
}
// We could write directly to shared data through an UnmanagedMemoryStream, and
// catch an error if not big enough. But then we don' get info about how much memory
// we needed, so instead we write to a dynamically resizing MemoryStream first.
MemoryStream^ memstream = gcnew MemoryStream();
BinaryFormatter^ formatter = gcnew BinaryFormatter();
formatter->Serialize(memstream, obj);
_length = (int)memstream->Length;
if (_length > sizeof(_data))
{
throw gcnew ApplicationException(String::Format("Not enough shared memory to transfer data (have {0} bytes, but {1} bytes required)", sizeof(_data), _length));
}
UnmanagedMemoryStream^ stream = gcnew UnmanagedMemoryStream(_data, _length, _length, FileAccess::Write);
memstream->WriteTo(stream);
memstream->Close();
stream->Close();
}
反序列化代码
Object^ Deserialize()
{
if (_length == 0)
{
return nullptr;
}
UnmanagedMemoryStream^ stream = gcnew UnmanagedMemoryStream(_data, _length, _length, FileAccess::Read);
BinaryFormatter^ formatter = gcnew BinaryFormatter();
Object^ obj = formatter->Deserialize(stream);
stream->Close();
return obj;
}
I need to serialize an object array array<object^>^
in C++/CLI which contains one intPtr
and a dictionary Dictionary<IntPtr,ObjectInfo>
where ObjectInfo
is some custom class. It is serializing properly but while deserializing it gives this error:
"System.Runtime.Serialization..SerializationException:Unable
to load type
System.Collections.Generic.Dictionary'2[[System.IntPtr,mscorlib,
Version=4.0.0.0",
Cultural=neutral,PublicKeyToken=..............],[System.Collection.Generic.List'1[[SharedObjects.ObjectInfo,SharedObjects,Version....]]
requires deserialization.
I am passing this array<object^>^
to Type->InvokeMember
as arguments, and these arguments need to be de-serialized before passing to InvokeMember
.
But it fails during deserialization.
Serialize Code:
void Serialize(Object^ obj)
{
if (obj == nullptr)
{
_length = 0;
return;
}
// We could write directly to shared data through an UnmanagedMemoryStream, and
// catch an error if not big enough. But then we don' get info about how much memory
// we needed, so instead we write to a dynamically resizing MemoryStream first.
MemoryStream^ memstream = gcnew MemoryStream();
BinaryFormatter^ formatter = gcnew BinaryFormatter();
formatter->Serialize(memstream, obj);
_length = (int)memstream->Length;
if (_length > sizeof(_data))
{
throw gcnew ApplicationException(String::Format("Not enough shared memory to transfer data (have {0} bytes, but {1} bytes required)", sizeof(_data), _length));
}
UnmanagedMemoryStream^ stream = gcnew UnmanagedMemoryStream(_data, _length, _length, FileAccess::Write);
memstream->WriteTo(stream);
memstream->Close();
stream->Close();
}
Deserialize Code
Object^ Deserialize()
{
if (_length == 0)
{
return nullptr;
}
UnmanagedMemoryStream^ stream = gcnew UnmanagedMemoryStream(_data, _length, _length, FileAccess::Read);
BinaryFormatter^ formatter = gcnew BinaryFormatter();
Object^ obj = formatter->Deserialize(stream);
stream->Close();
return obj;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了你的代码,它与自定义可序列化类完美配合。
您是否尝试使用简单的可序列化类来确保 _data 值在反序列化之前不会损坏?
您的序列化和反序列化代码是否在同一个程序集中?也许您有 2 个程序集引用包含自定义 ShareObject.ObjectInfo 类的程序集的不同版本。
这是我的测试代码:
I tried your code and it works perfectly with a custom serializable class.
Did you try your code with a simple serializable class to ensure _data value is not corrupt before deserialization?
Is your serialization and deserialization code in same assembly? Maybe you have 2 assemblies referencing different versions of your assembly containing your custom ShareObject.ObjectInfo class.
Here is my test code: