Hi,
I've created a UserControl-derived class called MyUserControl that is
able to persist and subsequently reload its state. It exposes two methods as
follows:
public void Serialize(Stream s);
public void Deserialize(Stream s);
Within the MyUserControl class, there is a field of type MyInnerClass
which is defined as follows:
private MyInnerClass MyInner=null;
...where MyInnerClass is defined as:
[Serializable()]
private class MyInnerClass{
public int Field;
public String Str;
}
When MyUserControl.Serialize() is called, 'MyInner' might be null or it
might not be. When it's not null, serialization takes place properly. When
it *is* null, serialization fails with an exception because
BinaryFormatter.Serialize() expects a non-null parameter.
What is the appropriate design pattern to use when serializing fields
which might be null? I'd like to avoid having to serialize an extra 'bool'
flag that indicates whether the field is null or not. Any ideas?
Thanks in advance,
David 5 24480
Hi David,
Are your
public void Serialize(Stream s);
public void Deserialize(Stream s);
implement by your self?
Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and
BinaryFormatter.Serialize(Stream s)?
You use the default serialization or customer your own behavior?
I have tried the default serialization of binaryformatter and it works well
with innerclass null.
I did not find the constrain that the BinaryFormatter.Serialize can only
serialize non-null object.
If you defined your own way of serialization, can you show the code to me ?
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: "David Sworder" <ds******@cts.com>
| Subject: How to Serialize 'null'?
| Date: Wed, 20 Aug 2003 16:28:54 -0700
| Lines: 36
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <uN**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups:
microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:177963
microsoft.public.dotnet.general:105256
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi,
|
| I've created a UserControl-derived class called MyUserControl that is
| able to persist and subsequently reload its state. It exposes two methods
as
| follows:
|
| public void Serialize(Stream s);
| public void Deserialize(Stream s);
|
| Within the MyUserControl class, there is a field of type MyInnerClass
| which is defined as follows:
|
| private MyInnerClass MyInner=null;
|
| ...where MyInnerClass is defined as:
|
| [Serializable()]
| private class MyInnerClass{
| public int Field;
| public String Str;
| }
|
| When MyUserControl.Serialize() is called, 'MyInner' might be null or
it
| might not be. When it's not null, serialization takes place properly. When
| it *is* null, serialization fails with an exception because
| BinaryFormatter.Serialize() expects a non-null parameter.
|
| What is the appropriate design pattern to use when serializing fields
| which might be null? I'd like to avoid having to serialize an extra 'bool'
| flag that indicates whether the field is null or not. Any ideas?
|
| Thanks in advance,
|
| David
|
|
|
Hi Jeffrey,
Yes, I implemented Serialize() and Deserialize() myself. These functions
eventually invoke the appropriate methods on the BinaryFormatter class. All
classes to be serialized are marked with the [Serializable()] attribute.
They do NOT implement ISerializable. The problem is occurring because
BinaryFormatter.Serialize() does not accept a null argument for the second
parameter. In other words, the top of the object graph cannot be null. You
can reproduce the error as follows:
ArrayList al=null;
new BinaryFormatter().Serialize(stream,al);
where 'stream' is a reference to a stream. In this trivial example, I'm
trying to serialize an ArrayList whose value is null. The exception thrown
is:
System.ArgumentNullException: Object Graph cannot be null.
Parameter name: graph
I can think of many different workarounds for this problem, but it seems
like the BinaryFormatter should be able to serialize a null field. If not,
what design pattern is typically used to circumvent this limitation. I
suppose the best approach would be to serialize a bit flag of some sort that
indicates which fields are null and therefore won't be written to the
stream.
Thanks again,
David
"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:bl**************@cpmsftngxa06.phx.gbl... Hi David,
Are your public void Serialize(Stream s); public void Deserialize(Stream s); implement by your self? Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and BinaryFormatter.Serialize(Stream s)? You use the default serialization or customer your own behavior?
I have tried the default serialization of binaryformatter and it works
well with innerclass null. I did not find the constrain that the BinaryFormatter.Serialize can only serialize non-null object.
If you defined your own way of serialization, can you show the code to me
? Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
-------------------- | From: "David Sworder" <ds******@cts.com> | Subject: How to Serialize 'null'? | Date: Wed, 20 Aug 2003 16:28:54 -0700 | Lines: 36 | X-Priority: 3 | X-MSMail-Priority: Normal | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 | Message-ID: <uN**************@TK2MSFTNGP12.phx.gbl> | Newsgroups: microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213 | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:177963 microsoft.public.dotnet.general:105256 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp | | Hi, | | I've created a UserControl-derived class called MyUserControl that
is | able to persist and subsequently reload its state. It exposes two
methods as | follows: | | public void Serialize(Stream s); | public void Deserialize(Stream s); | | Within the MyUserControl class, there is a field of type
MyInnerClass | which is defined as follows: | | private MyInnerClass MyInner=null; | | ...where MyInnerClass is defined as: | | [Serializable()] | private class MyInnerClass{ | public int Field; | public String Str; | } | | When MyUserControl.Serialize() is called, 'MyInner' might be null or it | might not be. When it's not null, serialization takes place properly.
When | it *is* null, serialization fails with an exception because | BinaryFormatter.Serialize() expects a non-null parameter. | | What is the appropriate design pattern to use when serializing
fields | which might be null? I'd like to avoid having to serialize an extra
'bool' | flag that indicates whether the field is null or not. Any ideas? | | Thanks in advance, | | David | | |
David,
PMFJI: I too get an exception in VS.NET 2003, with an outer object as null.
However I question the 'need' for workarounds on serializing the outer most
objects. Isn't having a work around to serialize the outer most object =
null, like saying: I do not have a Word document open/created yet, but I
need to be able to save the Word document? Note I am saying I do not have a
Word document, as opposed to having an empty Word document. An empty Word
document I can save...
Although you are saying MyInnerClass, is it really your outer class for the
serialization purposes?
If you let the formatter serialize the inner classes while it is serializing
the outer class you do not need 'work arounds' there either.
If I have | [Serializable()] | private class MyInnerClass{ | public int Field; | public String Str; | }
Elsewhere I would also have: | [Serializable()] | private class MyOuterClass{ | public MyInnerClass inner = null; | }
I would have an instance of MyOuterClass which I was attempting to
serialize.
I would use: MyOuterClass graph = new MyOuterClass(); new BinaryFormatter().Serialize(stream, graph);
The formatter, would serialize the MyOuterClass, while serializing
MyOuterClass, it will serialize the MyOuterClass.inner field, seeing as the
field is null, no inner object will be serialized. If MyOuterClass.inner had
an object reference, that object would be serialized.
The above is not intended as a work around, I'm explaining how I understand
serialization works.
Just confused & curious about what you are attempting as I am currently
working on serializing my objects.
Hope this helps
Jay
"David Sworder" <ds******@cts.com> wrote in message
news:Oe*************@tk2msftngp13.phx.gbl... Hi Jeffrey,
Yes, I implemented Serialize() and Deserialize() myself. These
functions eventually invoke the appropriate methods on the BinaryFormatter class.
All classes to be serialized are marked with the [Serializable()] attribute. They do NOT implement ISerializable. The problem is occurring because BinaryFormatter.Serialize() does not accept a null argument for the second parameter. In other words, the top of the object graph cannot be null. You can reproduce the error as follows:
ArrayList al=null; new BinaryFormatter().Serialize(stream,al);
where 'stream' is a reference to a stream. In this trivial example,
I'm trying to serialize an ArrayList whose value is null. The exception thrown is:
System.ArgumentNullException: Object Graph cannot be null. Parameter name: graph
I can think of many different workarounds for this problem, but it
seems like the BinaryFormatter should be able to serialize a null field. If not, what design pattern is typically used to circumvent this limitation. I suppose the best approach would be to serialize a bit flag of some sort
that indicates which fields are null and therefore won't be written to the stream.
Thanks again,
David
"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message news:bl**************@cpmsftngxa06.phx.gbl... Hi David,
Are your public void Serialize(Stream s); public void Deserialize(Stream s); implement by your self? Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and BinaryFormatter.Serialize(Stream s)? You use the default serialization or customer your own behavior?
I have tried the default serialization of binaryformatter and it works well with innerclass null. I did not find the constrain that the BinaryFormatter.Serialize can only serialize non-null object.
If you defined your own way of serialization, can you show the code to
me ? Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no
rights. -------------------- | From: "David Sworder" <ds******@cts.com> | Subject: How to Serialize 'null'? | Date: Wed, 20 Aug 2003 16:28:54 -0700 | Lines: 36 | X-Priority: 3 | X-MSMail-Priority: Normal | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 | Message-ID: <uN**************@TK2MSFTNGP12.phx.gbl> | Newsgroups: microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213 | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:177963 microsoft.public.dotnet.general:105256 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp | | Hi, | | I've created a UserControl-derived class called MyUserControl that is | able to persist and subsequently reload its state. It exposes two methods as | follows: | | public void Serialize(Stream s); | public void Deserialize(Stream s); | | Within the MyUserControl class, there is a field of type MyInnerClass | which is defined as follows: | | private MyInnerClass MyInner=null; | | ...where MyInnerClass is defined as: | | [Serializable()] | private class MyInnerClass{ | public int Field; | public String Str; | } | | When MyUserControl.Serialize() is called, 'MyInner' might be null
or it | might not be. When it's not null, serialization takes place properly. When | it *is* null, serialization fails with an exception because | BinaryFormatter.Serialize() expects a non-null parameter. | | What is the appropriate design pattern to use when serializing fields | which might be null? I'd like to avoid having to serialize an extra 'bool' | flag that indicates whether the field is null or not. Any ideas? | | Thanks in advance, | | David | | |
Hi Jay,
Thanks for your great comments.
Here is the situation: Recall that I have a UserControl-derived class.
Users of this class need to be able to persist the state of the control.
This is done via a Serialize() method that I expose on my control. The user
may close/open the app numerous times and at some point it's possible that
the app may need to create a new instance of my UserControl-derived class
and restore it to it's given state as specified by the information persisted
in the file. This is done via the Deserialize() method exposed by my
control.
Note that I'm *not* trying to serialize the control itself. This would
make no sense [you can't serialize the window handles, etc]. I'm just trying
to serialize the state. So, let's look at my Serialize() function. It starts
off like this:
private Serialize(Stream stream){
BinaryFormatter bf=new BinaryFormatter();
bf.Serialize(stream,innerObject);
bf.Serialize(stream,someObject);
}
Note that one or both of 'innerObject' and 'someObject' might be null or
non-null. If one (or more) of the objects is null, I need to record that
"nullness" to the stream so that when my Deserialize() method is called, the
null-ness is restored. Does this make sense? The following call:
bf.Serialize(stream,innerObject);
will throw an exception if 'innerObject' is null. See the problem?
Perhaps you're asking: If 'innerObject' is null, why serialize it at all?
Why not just use an 'if' condition to avoid serialization if 'innerObject'
is null? But that approach would corrupt the stream because my Deserialize()
method would have no idea whether or not to attempt deserialization of
"innerObject." That's why I'm considering the approach of serializing some
bit flags into the stream as a reminder at Deserialize-time of whether or
not certain objects should be deserialized or set to null.
I hope this makes sense.
David
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:uP****************@TK2MSFTNGP12.phx.gbl... David, PMFJI: I too get an exception in VS.NET 2003, with an outer object as
null. However I question the 'need' for workarounds on serializing the outer
most objects. Isn't having a work around to serialize the outer most object = null, like saying: I do not have a Word document open/created yet, but I need to be able to save the Word document? Note I am saying I do not have
a Word document, as opposed to having an empty Word document. An empty Word document I can save...
Although you are saying MyInnerClass, is it really your outer class for
the serialization purposes?
If you let the formatter serialize the inner classes while it is
serializing the outer class you do not need 'work arounds' there either.
If I have | [Serializable()] | private class MyInnerClass{ | public int Field; | public String Str; | } Elsewhere I would also have: | [Serializable()] | private class MyOuterClass{ | public MyInnerClass inner = null; | } I would have an instance of MyOuterClass which I was attempting to serialize.
I would use: MyOuterClass graph = new MyOuterClass(); new BinaryFormatter().Serialize(stream, graph); The formatter, would serialize the MyOuterClass, while serializing MyOuterClass, it will serialize the MyOuterClass.inner field, seeing as
the field is null, no inner object will be serialized. If MyOuterClass.inner
had an object reference, that object would be serialized.
The above is not intended as a work around, I'm explaining how I
understand serialization works.
Just confused & curious about what you are attempting as I am currently working on serializing my objects.
Hope this helps Jay
"David Sworder" <ds******@cts.com> wrote in message news:Oe*************@tk2msftngp13.phx.gbl... Hi Jeffrey,
Yes, I implemented Serialize() and Deserialize() myself. These functions eventually invoke the appropriate methods on the BinaryFormatter class. All classes to be serialized are marked with the [Serializable()] attribute. They do NOT implement ISerializable. The problem is occurring because BinaryFormatter.Serialize() does not accept a null argument for the
second parameter. In other words, the top of the object graph cannot be null.
You can reproduce the error as follows:
ArrayList al=null; new BinaryFormatter().Serialize(stream,al);
where 'stream' is a reference to a stream. In this trivial example, I'm trying to serialize an ArrayList whose value is null. The exception
thrown is:
System.ArgumentNullException: Object Graph cannot be null. Parameter name: graph
I can think of many different workarounds for this problem, but it seems like the BinaryFormatter should be able to serialize a null field. If
not, what design pattern is typically used to circumvent this limitation. I suppose the best approach would be to serialize a bit flag of some sort that indicates which fields are null and therefore won't be written to the stream.
Thanks again,
David
"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message news:bl**************@cpmsftngxa06.phx.gbl... Hi David,
Are your public void Serialize(Stream s); public void Deserialize(Stream s); implement by your self? Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and BinaryFormatter.Serialize(Stream s)? You use the default serialization or customer your own behavior?
I have tried the default serialization of binaryformatter and it works
well with innerclass null. I did not find the constrain that the BinaryFormatter.Serialize can
only serialize non-null object.
If you defined your own way of serialization, can you show the code to me ? Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no
rights. -------------------- | From: "David Sworder" <ds******@cts.com> | Subject: How to Serialize 'null'? | Date: Wed, 20 Aug 2003 16:28:54 -0700 | Lines: 36 | X-Priority: 3 | X-MSMail-Priority: Normal | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 | Message-ID: <uN**************@TK2MSFTNGP12.phx.gbl> | Newsgroups:
microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213 | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:177963 microsoft.public.dotnet.general:105256 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp | | Hi, | | I've created a UserControl-derived class called MyUserControl
that is | able to persist and subsequently reload its state. It exposes two methods as | follows: | | public void Serialize(Stream s); | public void Deserialize(Stream s); | | Within the MyUserControl class, there is a field of type MyInnerClass | which is defined as follows: | | private MyInnerClass MyInner=null; | | ...where MyInnerClass is defined as: | | [Serializable()] | private class MyInnerClass{ | public int Field; | public String Str; | } | | When MyUserControl.Serialize() is called, 'MyInner' might be
null or it | might not be. When it's not null, serialization takes place
properly. When | it *is* null, serialization fails with an exception because | BinaryFormatter.Serialize() expects a non-null parameter. | | What is the appropriate design pattern to use when serializing fields | which might be null? I'd like to avoid having to serialize an extra 'bool' | flag that indicates whether the field is null or not. Any ideas? | | Thanks in advance, | | David | | |
David, to serialize the state. So, let's look at my Serialize() function. It
starts off like this:
private Serialize(Stream stream){ BinaryFormatter bf=new BinaryFormatter(); bf.Serialize(stream,innerObject); bf.Serialize(stream,someObject); }
Actually it would help if we saw where it really started, where the stream
itself is created. :-)
I suspect where it really starts, you create a stream object, then you call
Serialize for each of your UserControls. Correct?
Causing multiple serializations to the same stream. Correct?
Do you always call each user control in same exact same order when you
serialize & deserialize them? If you don't you may have problems later...
Although it makes sense in the context of your design, I'm not convinced
that BinaryFormatter.Serialize should accept a null for the graph either.
I also do not think you need to find a 'workaround' for
BinaryFormatter.Serialize not accepting a null.
Which brings us to your design, not that your design is flawed or anything.
Its simple straight forward, easy to follow. Which is a good thing.
When you use someObject & innerObject in your user controls are you checking
for null each time you use them?
I'm having two somewhat completely different thoughts here, neither of which
may really work for you.
1. Consider using a Special Case Pattern for someObject & innerObject. http://www.martinfowler.com/eaaCatalog/specialCase.html
Instead of allowing null to be stored in these variables and checking them
for null before each use. Have a NullObject, an object that derives from the
MyInnerClass (or same base class) that returns the same values you would
have used if the variable was null. Your serialization logic can then stay
put. NullObject could be as simple as a singleton in the MyInnerClass
similar to String.Empty. However on deserialization you then need to worry
about getting back to the specific singleton. The IObjectReference interface
is useful in this regard. If NullObject was its own class derived from
MyInnerClass this would not be as big a problem, even then I would make this
new class a singleton. http://msdn.microsoft.com/library/de...ClassTopic.asp
2. Consider having the Serialize & Deserialize methods accept a HashTable
(or something) instead. Adding or Getting someObject & innerObject from the
HashTable. I would consider using Control.Name as part of the key. Then
where the Stream is created, I would create a HashTable call all the
UserControl.Serialize methods, create the stream, serialize the hashtable,
close the stream.
Hope this helps
Jay
"David Sworder" <ds******@cts.com> wrote in message
news:OG**************@TK2MSFTNGP10.phx.gbl... Hi Jay,
Thanks for your great comments.
Here is the situation: Recall that I have a UserControl-derived class. Users of this class need to be able to persist the state of the control. This is done via a Serialize() method that I expose on my control. The
user may close/open the app numerous times and at some point it's possible that the app may need to create a new instance of my UserControl-derived class and restore it to it's given state as specified by the information
persisted in the file. This is done via the Deserialize() method exposed by my control.
Note that I'm *not* trying to serialize the control itself. This would make no sense [you can't serialize the window handles, etc]. I'm just
trying to serialize the state. So, let's look at my Serialize() function. It
starts off like this:
private Serialize(Stream stream){ BinaryFormatter bf=new BinaryFormatter(); bf.Serialize(stream,innerObject); bf.Serialize(stream,someObject); }
Note that one or both of 'innerObject' and 'someObject' might be null
or non-null. If one (or more) of the objects is null, I need to record that "nullness" to the stream so that when my Deserialize() method is called,
the null-ness is restored. Does this make sense? The following call:
bf.Serialize(stream,innerObject);
will throw an exception if 'innerObject' is null. See the problem? Perhaps you're asking: If 'innerObject' is null, why serialize it at all? Why not just use an 'if' condition to avoid serialization if 'innerObject' is null? But that approach would corrupt the stream because my
Deserialize() method would have no idea whether or not to attempt deserialization of "innerObject." That's why I'm considering the approach of serializing some bit flags into the stream as a reminder at Deserialize-time of whether or not certain objects should be deserialized or set to null.
I hope this makes sense.
David
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in
message news:uP****************@TK2MSFTNGP12.phx.gbl... David, PMFJI: I too get an exception in VS.NET 2003, with an outer object as null. However I question the 'need' for workarounds on serializing the outer
most objects. Isn't having a work around to serialize the outer most object = null, like saying: I do not have a Word document open/created yet, but I need to be able to save the Word document? Note I am saying I do not
have a Word document, as opposed to having an empty Word document. An empty
Word document I can save...
Although you are saying MyInnerClass, is it really your outer class for the serialization purposes?
If you let the formatter serialize the inner classes while it is serializing the outer class you do not need 'work arounds' there either.
If I have > | [Serializable()] > | private class MyInnerClass{ > | public int Field; > | public String Str; > | }
Elsewhere I would also have: > | [Serializable()] > | private class MyOuterClass{ > | public MyInnerClass inner = null; > | }
I would have an instance of MyOuterClass which I was attempting to serialize.
I would use: MyOuterClass graph = new MyOuterClass(); new BinaryFormatter().Serialize(stream, graph);
The formatter, would serialize the MyOuterClass, while serializing MyOuterClass, it will serialize the MyOuterClass.inner field, seeing as the field is null, no inner object will be serialized. If MyOuterClass.inner had an object reference, that object would be serialized.
The above is not intended as a work around, I'm explaining how I understand serialization works.
Just confused & curious about what you are attempting as I am currently working on serializing my objects.
Hope this helps Jay
"David Sworder" <ds******@cts.com> wrote in message news:Oe*************@tk2msftngp13.phx.gbl... Hi Jeffrey,
Yes, I implemented Serialize() and Deserialize() myself. These functions eventually invoke the appropriate methods on the BinaryFormatter
class. All classes to be serialized are marked with the [Serializable()]
attribute. They do NOT implement ISerializable. The problem is occurring because BinaryFormatter.Serialize() does not accept a null argument for the
second parameter. In other words, the top of the object graph cannot be null. You can reproduce the error as follows:
ArrayList al=null; new BinaryFormatter().Serialize(stream,al);
where 'stream' is a reference to a stream. In this trivial
example, I'm trying to serialize an ArrayList whose value is null. The exception thrown is:
System.ArgumentNullException: Object Graph cannot be null. Parameter name: graph
I can think of many different workarounds for this problem, but it seems like the BinaryFormatter should be able to serialize a null field. If not, what design pattern is typically used to circumvent this limitation. I suppose the best approach would be to serialize a bit flag of some
sort that indicates which fields are null and therefore won't be written to the stream.
Thanks again,
David
"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message news:bl**************@cpmsftngxa06.phx.gbl... > > Hi David, > > Are your > public void Serialize(Stream s); > public void Deserialize(Stream s); > implement by your self? > Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and > BinaryFormatter.Serialize(Stream s)? > You use the default serialization or customer your own behavior? > > I have tried the default serialization of binaryformatter and it
works well > with innerclass null. > I did not find the constrain that the BinaryFormatter.Serialize can
only > serialize non-null object. > > If you defined your own way of serialization, can you show the code
to me ? > > Best regards, > Jeffrey Tan > Microsoft Online Partner Support > Get Secure! - www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights. > > -------------------- > | From: "David Sworder" <ds******@cts.com> > | Subject: How to Serialize 'null'? > | Date: Wed, 20 Aug 2003 16:28:54 -0700 > | Lines: 36 > | X-Priority: 3 > | X-MSMail-Priority: Normal > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 > | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 > | Message-ID: <uN**************@TK2MSFTNGP12.phx.gbl> > | Newsgroups: > microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp > | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213 > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl > | Xref: cpmsftngxa06.phx.gbl > microsoft.public.dotnet.languages.csharp:177963 > microsoft.public.dotnet.general:105256 > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp > | > | Hi, > | > | I've created a UserControl-derived class called MyUserControl that is > | able to persist and subsequently reload its state. It exposes two methods > as > | follows: > | > | public void Serialize(Stream s); > | public void Deserialize(Stream s); > | > | Within the MyUserControl class, there is a field of type MyInnerClass > | which is defined as follows: > | > | private MyInnerClass MyInner=null; > | > | ...where MyInnerClass is defined as: > | > | [Serializable()] > | private class MyInnerClass{ > | public int Field; > | public String Str; > | } > | > | When MyUserControl.Serialize() is called, 'MyInner' might be null or > it > | might not be. When it's not null, serialization takes place properly. When > | it *is* null, serialization fails with an exception because > | BinaryFormatter.Serialize() expects a non-null parameter. > | > | What is the appropriate design pattern to use when serializing fields > | which might be null? I'd like to avoid having to serialize an
extra 'bool' > | flag that indicates whether the field is null or not. Any ideas? > | > | Thanks in advance, > | > | David > | > | > | >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David Sworder |
last post by:
Hi,
I've created a UserControl-derived class called MyUserControl that is
able to persist and subsequently reload its state. It exposes two methods as
follows:
public void Serialize(Stream...
|
by: andrewcw |
last post by:
I have an object to serialize.
TextWriter writer = new StreamWriter("test.xml");
serializer.Serialize(writer,obj);
writer.Close();
but below does not, why ?? I have a file that I will have...
|
by: Dan |
last post by:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That
public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;...
|
by: meh |
last post by:
Still have not been able to convert this. More importently.....Is this
sample going about it the right way???
tia
meh
Here is the vb code.........I keep looking at older vb.net projects to...
|
by: Jerry |
last post by:
Hi,
I have a class like the following:
class A {
private B _b;
A (B b) {
_b = b;
}
...
|
by: Mirek Endys |
last post by:
What is the best way to serialize object, that contains data in interfaces.
Simple. I have instance of my object, that contains data in interfaces.
What is the best way to XMLSerialize and...
|
by: yusuf |
last post by:
I basically want to be able to store and retrieve a tree structure in
greasemonkey. Unfortunately the GM_setValue and GM_getValue functions
only take string key value pairs. Is there a native...
|
by: job |
last post by:
how is it possible to serialize/de-serialize a SqlCommand?
|
by: =?Utf-8?B?Qm9uaQ==?= |
last post by:
Hi,
I have a class to serialize. This class has come properties like this one
private myObject _listOf;
public myObject listOf
{
get {
if(_listOf==null)
{...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |