如何封送对象及其内容(也是对象)也是、对象、内容

2023-09-07 09:39:41 作者:蓉嬷嬷当年一枝花

我有一个问题要问我怀疑,答案是有点复杂。在这一刻,我编程的DLL(类库)在C#中。该DLL使用第三方库,因此与我没有源$ C ​​$ C第三方对象处理。现在,我计划再创建一个DLL,这将被用来在后面的体育场在我的应用程序。第二个DLL应该使用的第一个DLL创建的第三方对象(具有相应的对象状态)。

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.

幸运的是,第三方对象扩展 MarshalByRefObject的类。我可以使用编组对象 System.Runtime.Remoting.Marshal(...)。然后,我使用的是的BinaryFormatter 序列化对象和存储对象为字节[] 阵列。一切顺利的话。我可以用相反的方式反序列化和反编组,并最终与我原来的第三方对象......所以才出现...

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.

现在我的世界的问题:如何将我去编组/序列化所有这些我的第三方对象引用的对象......并逐级下排参考树获得全面和完整的序列化对象?现在我的猜测是preprocess:获取所有的对象,并建立自己的自定义对象和序列化。但我希望有一些其他的方式...

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...

推荐答案

这是一个有点我不清楚为什么你需要两个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.

如果本机DLL公开它的功能可以使用的P / Invoke调用,在大多数情况下封送处理会为你做; 如果本机DLL公开其方法为COM接口,您可以创建COM包装; 如果你必须尽一切手动,您可能需要使用的 LayoutKind 和 FieldOffsetAttribute 或 StructLayoutAttribute ,这些属性帮助您告诉编译器如何对象的内存布局; 看一看 MarshalAsAttribute 和 UnmanagedType ,它可能正是你需要的。 If the native DLL exposes its functions you can use P/Invoke calls and in most cases the marshaling will be done for you; If the native DLL exposes its methods as COM interfaces, you can create COM wrappers; 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; Have a look at MarshalAsAttribute and UnmanagedType, it may be just what you need.