Microsoft KB Archive/818412

= BUG: &quot;System.Runtime.Serialization.SerializationException&quot; exception if you declare a static local variable in a method of a Visual Basic .NET or Visual Basic 2005 class and try to serialize the object of the class by using BinaryFormatter or SoapFormatte =

Article ID: 818412

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



SYMPTOMS
Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 allows you to declare static local variables inside a method of class. However, if you declare a static local variable in a method of the Visual Basic .NET or Visual Basic 2005 class, and then you try to serialize the object of the class by using BinaryFormatter or SoapFormatter, you receive the following exception during run time:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

Additional information: The type Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag in Assembly Microsoft.VisualBasic, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a is not marked as serialized.



CAUSE
The common language runtime does not support static variables in methods. When Visual Basic .NET or Visual Basic 2005 compiler compiles your code, it translates this high-level Visual Basic .NET or Visual Basic 2005 code to Microsoft intermediate language (MSIL) code that the common language runtime can understand to give you shared variable functionality. Visual Basic .NET or Visual Basic 2005 uses the Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag class to provide this functionality. In Visual Basic .NET or in Visual Basic 2005 compiler, the StaticLocalInitFlag is not marked as serialized. Therefore, you receive the exception during run time.



RESOLUTION
You cannot resolve this bug by using BinaryFormatter or SoapFormatter. To resolve this bug, use XmlSerializer instead of using BinaryFormatter or SoapFormatter to serialize the object. However, you may not find an entry for the local static variable in the generated XML file. The following code describes the usage of XmlSerializer to serialize the object: Imports System.IO Imports System.Xml.Serialization

Module Module1

Sub Main Dim myObj As New MyTestClass Dim myStream As Stream Dim mySerializer As New XmlSerializer(GetType(MyTestClass)) Try myStream = File.Open(&quot;C:\obj.xml&quot;, FileMode.OpenOrCreate, FileAccess.ReadWrite) mySerializer.Serialize(myStream, myObj) Finally myStream.Close End Try

End Sub End Module

Public Class MyTestClass

Public Sub New End Sub

Public Sub myTestMethod Static myStaticVar As Integer = 1 End Sub End Class



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click to select Visual Basic .NET Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click to select Console Application. Name the project MyConsoleApplication, and then click OK.  Replace the existing code with the following code: Imports System.IO Imports System.Runtime.Serialization.Formatters.Binary

Module Module1

Sub Main Dim myObj As New MyTestClass Dim myStream As Stream Dim formatter As New BinaryFormatter Try myStream = File.Open(&quot;C:\obj.xml&quot;, FileMode.OpenOrCreate, FileAccess.ReadWrite) formatter.Serialize(myStream, myObj) Finally myStream.Close End Try End Sub End Module

Public Class MyTestClass

Public Sub New End Sub

Public Sub myTestMethod Static myStaticVar As Integer = 1 End Sub End Class </li> On the Debug menu, click Start.

You receive the exception mentioned in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">