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 (818412)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SYMPTOMSMicrosoft 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. CAUSEThe 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. RESOLUTIONYou 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.
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
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbvs2002sp1sweep kberrmsg kbXML kbSerial kbbug KB818412 kbAudDeveloper |
---|
|