views:

58

answers:

1

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

A: 

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.
Abel
Well I need two assemblies, because there are two processes which need to be initiated at two seperate moments in time. The first assembly produces an object (the 3rd party object). At an undecided moment in time in the future the other assembly has to use the information in the object. Which - in my view - means the object has to be serialized to be able to store it.Thanks for your reply. I will look into your suggestions.
Waldo Spek