473,769 Members | 6,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fast serialization for third party object

I have integrated APIs from a third party into my framework. The third
party is essentially a .Net wrapper on top of an existing set of C++
classes and structs.

I want to pass-around objects in these native structures as much as
possible - that is I don't want to fool around with object
serialization/deserialization . Data rates approach several hundred
thousand objects per second, hence I simply can't pay the performance
penalty this would entail. All of the structures are fixed-size
formats, so simply copying byte-arrays from structure to structure is
my preferred approach.

Unfortunately, the third party neglected to mark one important class as
serializable so I have followed the 'Manual Base Class Serialization'
pattern set down in the Advanced Serialization article:

http://msdn.microsoft.com/msdnmag/is...Serialization/

For instance:

public class ThirdPartyClass ()
{
}

[Serializable]
public class MySerializableC lass() : ThirdPartyClass
{
public void GetObjectData(S erializationInf o info, StreamingContex t
context)
{
SerializationUt il.SerializeBas eType(this, info, context); // (see
article for details)
}
}

However, because C# has no built in mechanism to coerce a downcast, it
seems I need to manually force the coersion. That is:

ThirdPartyClass thirdPartyObj = ThirdPartyAPI.G etObject();
MySerializableC lass myObj = (MySerializable Class)thirdPart yObj; <<<
THROWS InvalidCastExce ption

throws an exception at the attempted downcast.

I have worked out a brute force technique to force the downcast by
marshaling the object into an unmanaged array of bytes and back again,
ala:

[Serializable]
[StructLayout(La youtKind.Sequen tial)]
public MySerializableC lass
{
public static MySerializableC lass CloneFrom(Third PartyClass
thirdPartyObj)
{
IntPtr ptrOut = Marshal.AllocHG lobal(Marshal.S izeOf(thirdPart yObj));
Marshal.Structu reToPtr(thirdPa rtyObj, ptrOut, true);
MySerializableC lass wrapper = new MySerializableC lass();
Marshal.PtrToSt ructure(ptrOut, wrapper);
Marshal.FreeHGl obal(ptrOut);
return wrapper;
}
}

Thus, a quick call to this method returns a serializable object that
can be passed around between processes:

MySerializableC lass myObj =
MySerializableC lass.CloneFrom( thirdPartyObj);
aRemotable.Some Method(myObj);

QUESTION: Is this the correct technique for what I'm trying to do?
Also, I'm confused why the I need to include the attribute
[StructLayout(La youtKind.Sequen tial)] for this to work.

All feedback is appreciated (yes, I know this is not the 'approved'
..Net way of handling things).

May 22 '06 #1
0 3036

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1224
by: Roland Hall | last post by:
I stated to someone in a different post, perhaps different NG, that since I have 3rd party cookies blocked in IE and after a recent windows security update, ads with 3rd party cookies do not load the page. This is the link I went to: http://redmondmag.com/columns/article.asp?EditorialsID=141 The page cannot be found The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.
3
2121
by: Toto | last post by:
Gray Davis is a desperate man. Facing recall, he has just put the safety of California motorists at risk in a naked bid to buy Hispanic votes. Davis signed into law a bill he had twice rejected, to allow illegal aliens to get driver's licenses. One million illegals, many of them young, single males, will soon be on the roads of California. Inevitably, the toll of traffic dead will soar and California families will pay in the lives of...
20
9171
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
7
9558
by: schoenfeld1 | last post by:
I've implemented IPC between two applications using named pipes and binary serialization, but have noticed that the binary formatter is rather slow. It seems that the binary formatter reflects the entire type everytime it is invoked to serialize/deserialize an object of that type. Is there a way to prepare the binary formatter with a pre-defined type, such that it only reflects once but can be re-used to serialize/deserialize objects...
2
8806
by: RJ | last post by:
I've been trying to find documentation that explains limitation of possible types that a webmethod can return. I realize xml serialization must take place to return the specified type. Must the return type of a webmethod implement ISerializable? I saw something about using SoapInclude or XMLInclude attribute to include other types in web services. Does this imply that it is possible to return any type, even some third party class? ...
8
2461
by: jamie | last post by:
Here's a class I'm working with public class BatchHeader { public string ScaleSite; public string FromScaleDate;
0
1144
by: =?Utf-8?B?b3h5Z2Vub25lQGdtYWlsLmNvbQ==?= | last post by:
Hey everyone. I am developing a C# program that launches various third-party applications (applications I do not have the source code to) with various inputs. Some of those inputs could cause the third-party application to crash. That is fine, except I do not want the Error Reporting Window that Windows pops up to be seen by the user (I want to handle it myself). The Error Reporting window I am talking about is the window that comes up...
11
3690
by: William | last post by:
I'm looking for an example that would show how to serialize a c++ object at it's simplest w/o using any other api's. I have a class that I want to serialize and then pass to my obj-c class so I can send it over the wire. I'm just looking for how to serialize it, then pack it back up on the other end. Any help much appreciated.
0
1203
by: samh | last post by:
Hi, I'm using .net 1.1 and have a web reference to a 3rd party. What I'm attempting to do is when I get a "failed" response from the webservice (that's inserting some data or doing something else) I want to serialise the response object to disk. So for instance in reference.vb I have the lines
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.