Serailizing Static Variables Problems

J

JackRazz

I'm trying to serialize a class using the binary formatter without serializing the
'static pastFirstCall as Boolean' variable. Normally private (module-scope)
variables are serialized unless the <NonSerialized> attribute is used.

Public Function NextMessageNo() As Integer
<NonSerialized()> Static pastFirstCall As Boolean = False 'This is
being Serialized!!!!!!!!!!
If Not pastFirstCall Then
mNextMessageNumber = mNextPersistableMessageNo
pastFirstCall = True
End If
NextMessageNo = mNextMessageNumber + 1
mNextMessageNumber = NextMessageNo
End Function
<NonSerialized()> Private mNextMessageNumber As Integer


Using Reflector, I can see that, internally, pastFirstCall is declared as a private
variable as:
Private $STATIC$NextMessageNo$2008$pastFirstCall As BooleanThe problem is that the
<NonSerialized> attribute is not carried forward to this declaration. Are there any
workarounds other than changing the delcaration to private. Has anyone written a
custom attribute to fix it? I have a ton of static's (which I like because they
limit the scope, thus reducing the complexity) and don't want to go thru all my code
changing them to private module-scope declarations. Almost forgot, I'm using the
..NET Framework 1.0 with the latest service packs. Any Ideas?

Thanks - JackRazz
 
N

Nicholas Paldino [.NET/C# MVP]

Jack,

There really isn't a way to get around this, you will have to move this
to the module level as there is no way to declare the attribute properly on
the variable when it is static in a method call.

Hope this helps.
 
J

JackRazz

Nicholas,
Thanks for the info. Did they ever fix it with the 1.1 framework?




| Jack,
|
| There really isn't a way to get around this, you will have to move this
| to the module level as there is no way to declare the attribute properly on
| the variable when it is static in a method call.
|
| Hope this helps.
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - (e-mail address removed)
|
| | > I'm trying to serialize a class using the binary formatter without
| > serializing the
| > 'static pastFirstCall as Boolean' variable. Normally private
| > (module-scope)
| > variables are serialized unless the <NonSerialized> attribute is used.
| >
| > Public Function NextMessageNo() As Integer
| > <NonSerialized()> Static pastFirstCall As Boolean = False
| > 'This is
| > being Serialized!!!!!!!!!!
| > If Not pastFirstCall Then
| > mNextMessageNumber = mNextPersistableMessageNo
| > pastFirstCall = True
| > End If
| > NextMessageNo = mNextMessageNumber + 1
| > mNextMessageNumber = NextMessageNo
| > End Function
| > <NonSerialized()> Private mNextMessageNumber As Integer
| >
| >
| > Using Reflector, I can see that, internally, pastFirstCall is declared as
| > a private
| > variable as:
| > Private $STATIC$NextMessageNo$2008$pastFirstCall As BooleanThe problem is
| > that the
| > <NonSerialized> attribute is not carried forward to this declaration. Are
| > there any
| > workarounds other than changing the delcaration to private. Has anyone
| > written a
| > custom attribute to fix it? I have a ton of static's (which I like
| > because they
| > limit the scope, thus reducing the complexity) and don't want to go thru
| > all my code
| > changing them to private module-scope declarations. Almost forgot, I'm
| > using the
| > .NET Framework 1.0 with the latest service packs. Any Ideas?
| >
| > Thanks - JackRazz
| >
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top