Microsoft KB Archive/818412

From BetaArchive Wiki
Knowledge Base


BUG: "System.Runtime.Serialization.SerializationException" 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("C:\obj.xml", 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.

MORE INFORMATION

Steps to Reproduce the Behavior

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

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click to select Console Application.
  5. Name the project MyConsoleApplication, and then click OK.
  6. 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("C:\obj.xml", 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
  7. On the Debug menu, click Start.

    You receive the exception mentioned in the "Symptoms" section of this article.


REFERENCES

For more information about XML Serialization, click the following article numbers to view the articles in the Microsoft Knowledge Base:

323503 WebCast: XML Serialization and Sample Code


314150 INFO: Roadmap for XML Serialization in the .NET Framework




Keywords: kbvs2005swept kbvs2005applies kbvs2002sp1sweep kberrmsg kbxml kbserial kbbug KB818412