473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 MySerializableClass() : ThirdPartyClass
{
public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
SerializationUtil.SerializeBaseType(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.GetObject();
MySerializableClass myObj = (MySerializableClass)thirdPartyObj; <<<
THROWS InvalidCastException

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(LayoutKind.Sequential)]
public MySerializableClass
{
public static MySerializableClass CloneFrom(ThirdPartyClass
thirdPartyObj)
{
IntPtr ptrOut = Marshal.AllocHGlobal(Marshal.SizeOf(thirdPartyObj) );
Marshal.StructureToPtr(thirdPartyObj, ptrOut, true);
MySerializableClass wrapper = new MySerializableClass();
Marshal.PtrToStructure(ptrOut, wrapper);
Marshal.FreeHGlobal(ptrOut);
return wrapper;
}
}

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

MySerializableClass myObj =
MySerializableClass.CloneFrom(thirdPartyObj);
aRemotable.SomeMethod(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(LayoutKind.Sequential)] 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 3010

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

Similar topics

2
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...
3
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...
20
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.,...
7
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...
2
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...
8
by: jamie | last post by:
Here's a class I'm working with public class BatchHeader { public string ScaleSite; public string FromScaleDate;
0
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...
11
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...
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.