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:
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
- 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("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
- 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