如何编组对象及其内容(也是对象)

发布于 2024-09-06 07:10:21 字数 598 浏览 5 评论 0原文

我有一个问题,我怀疑答案有点复杂。目前我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2024-09-13 07:10:21

我有点不清楚为什么你需要两个 C# 程序集来处理第三方 DLL,你创建的第一个类库不是已经与第三方 DLL 连接了吗?以下是处理本机库时的一些一般答案。如果第三方库是公开可用的,那么查看它使用的接口将会有所帮助。

  1. 如果本机 DLL 公开其函数,您可以使用 P/Invoke 调用,并且在大多数情况下,将为您完成编组;
  2. 如果本机 DLL 将其方法公开为 COM 接口,您可以创建 COM 包装器;
  3. 如果您必须手动完成所有操作,则可能需要使用 LayoutKindFieldOffsetAttributeStructLayoutAttribute,这些属性有帮助你告诉编译器该对象的内部内存布局如何;
  4. 查看 MarshalAsAttributeUnmanagementType,它可能正是您所需要的。

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.

  1. If the native DLL exposes its functions you can use P/Invoke calls and in most cases the marshaling will be done for you;
  2. If the native DLL exposes its methods as COM interfaces, you can create COM wrappers;
  3. If you must do everything by hand, you might need to use the LayoutKind and FieldOffsetAttribute or the StructLayoutAttribute, these attribute help you tell the compiler how the internal memory layout of the object is;
  4. Have a look at MarshalAsAttribute and UnmanagedType, it may be just what you need.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文