Re: Write class/struct object to file



Thanks for Sam's input.

Hi Ashutosh,

In .net framework, the code can not directly access memory which is totally
different from c/c++. That means you can not write out class/struct objects
into memory/file through their point/address. If you want to persist some
class objects into memory or file(in binary format), you can use the Binary
Serialization feature as Sam suggested.

To do binary serialziation on class, you need to first make the certain
class serializable and then use Binary Serializer to serialize the
instances of those classes. You can serialize them into memoryStream or
fileStream.

Here are some articles introducing .net binary serialization:

#Binary Serialization
http://msdn2.microsoft.com/en-us/library/72hyey7b(VS.71).aspx

#Run-time Serialization
http://msdn.microsoft.com/msdnmag/issues/02/04/net/

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: Samuel R. Neff <samuelneff@xxxxxxxxxx>
Subject: Re: Write class/struct object to file
Date: Fri, 25 Jan 2008 17:20:54 -0500


Are you trying to match a format used by another application or simply
store a class/struct as binary data? If the latter, then
BinaryFormatter to Serialize/Deserialize the object to/form the
stream.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.




On Sat, 19 Jan 2008 02:29:29 +0530, "Ashutosh"
<smbs_user@xxxxxxxxxxxxxxxx> wrote:

Hi,
How can I create a fixed size class or structure which consist of members
of
any types and strings of fixed (specified) sizes. I need to write the
class
object by using a BinaryStream....

I need to keep the code fully managed. I tried to declare the
array/string
of fixed size, but the compiler says to make to code unmanaged/unsafe or
use
Marshal attributes. I know how to use those attributes, but as far as I
think they doesn't make sense here...

Actually I am looking for something similar to this in C++
---------------------------------------------------------
CFile f;
f.Open(....);

struct someStruct s1;

f.Write((char*)&s1,sizeof(s1));
---------------------------------------------------------
Any ideas how I can achieve the same in C# with minimal effort?

Regards,
Ashutosh




.



Relevant Pages

  • Re: How do I measure the size of a dataset?
    ... I am concern about the web server memory that this dataset might occupy. ... The memory that a dataset occupies might not be equal with the size of the dataset's serialization result ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Definition of Serialization.
    ... The following article has a good definition of serialization. ... take an "out of memory" snap shot of the object & persist it someplace else. ... Stream or other object that persists data. ... Binary Serialization: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: share a structure array containing multidimensional char array
    ... When you want to deserialize/serialize a managed type to/from unmanaged memory, you have to use the same serializer/deserializer at both sides, that means you also need to use .NET to serialize/deserialize at the C++ side. ... Besides the structures, you also have to store the number of structures serialized to the shared memory buffer and the size of the individual structure members, without this info it's impossible for the reader to determine the number of array members and the size of the structure members. ... HANDLE hKernel; ... > Mapping a datastructure, whatever it's type, in a serialization. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: share a structure array containing multidimensional char array
    ... memory, you have to use the same serializer/deserializer at both sides, that ... Deserialization Error: ... C++ code doesnot make any serialization. ... instance as an int value at the start of the MM segment. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Serializing Partial Classes (Datasets)
    ... Both partial classes and serialization are good ideas -- too bad you can't ... Customer Support Services via telephone so that a dedicated Support ... it will use the GetObjectData method ... you extend the Checklists class by adding another partial ...
    (microsoft.public.dotnet.framework)