如何编组对象及其内容(也是对象)
我有一个问题,我怀疑答案有点复杂。目前我正在用 C# 编写 DLL(类库)。该 DLL 使用第 3 方库,因此处理我没有源代码的第 3 方对象。现在我计划创建另一个 DLL,它将在我的应用程序中的稍后体育场中使用。第二个 DLL 应使用第一个 DLL 创建的第三方对象(具有相应的对象状态)。
幸运的是,第 3 方对象扩展了 MarshalByRefObject
类。我可以使用 System.Runtime.Remoting.Marshal(...) 编组对象。然后,我使用 BinaryFormatter
序列化对象,并将对象存储为 byte[]
数组。一切顺利。我可以以相反的方式反序列化和解组,最终得到我原来的第 3 方对象...所以看起来...
不过,当在我的第 3 方反序列化对象上调用方法时,我得到对象内部异常。通常这些方法返回其他第 3 方对象,但是(显然 - 我猜)现在这些对象丢失了,因为它们没有序列化。
现在我的全局问题:我将如何编组/序列化我的第 3 方对象引用的所有对象......并向下级联“引用树”以获得完整且完整的序列化对象?现在我的猜测是预处理:获取所有对象并构建我自己的自定义对象并将其序列化。但我希望还有其他方法...
I have a question for which I suspect the answer is a bit complex. At this moment I am programming a DLL (class library) in C#. This DLL uses a 3rd party library and therefore deals with 3rd party objects of which I do not have the source code. Now I am planning to create another DLL, which is going to be used in a later stadium in my application. This second DLL should use the 3rd party objects (with corresponding object states) created by the first DLL.
Luckily the 3rd party objects extend the MarshalByRefObject
class. I can marshal the objects using System.Runtime.Remoting.Marshal(...)
. I then serialize the objects using a BinaryFormatter
and store the objects as a byte[]
array. All goes well. I can deserialize and unmarshal in a the opposite way and end up with my original 3rd party objects...so it appears...
Nevertheless, when calling methods on my 3rd party deserialized objects I get object internal exceptions. Normally these methods return other 3rd party objects, but (obviously - I guess) now these objects are missing because they weren't serialized.
Now my global question: how would I go about marshalling/serializing all the objects which my 3rd party objects reference...and cascade down the "reference tree" to obtain a full and complete serialized object? Right now my guess is to preprocess: obtain all the objects and build my own custom object and serialize it. But I'm hoping there is some other way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有点不清楚为什么你需要两个 C# 程序集来处理第三方 DLL,你创建的第一个类库不是已经与第三方 DLL 连接了吗?以下是处理本机库时的一些一般答案。如果第三方库是公开可用的,那么查看它使用的接口将会有所帮助。
It's a bit unclear to me why you need two C# assemblies to deal with the third party DLL, isn't the first class library you created already interfacing your third party DLL? Here are some general answers when dealing with native libraries. It would help if the third party library is publicly available, to see what interfaces it uses.