问题描述
非常感谢您的阅读;
环境:
- Windows 10 Home
- Visual Studio Professional 2015
- VB.NET
我有一个名为"StateRecord"的类,这样可以保存对象的状态.我在FormMain中声明了此类的实例,然后继续将状态信息存储到该实例.在StateRecord.SaveState函数中,我是维 名为"graph"的变量;作为type = StateRecord.
I have a class called "StateRecord" which enables saving the state of objects. I declare an instance of this class in FormMain then proceed to store state information to this instance. Within the StateRecord.SaveState function I dimension a variable named "graph" as type=StateRecord.
Dim graph As StateRecord = Nothing
在StateRecord的SaveState函数中,将图形变量设置为"myself":
In the SaveState function of StateRecord, I set the graph variable to "myself":
graph = Me
当我尝试序列化图形时,错误指出FormMain.vb没有标记为序列化.尽管我已经在FormMain中实例化了StateRecord对象,并且FormMain调用了StateRecord.SaveState函数,但StateRecord类是一个单独的实例 文件,并且仅在FormMain中实例化. 我也不尝试序列化FormMain,仅尝试序列化它的某些方面,例如其当前大小和位置,当前显示的Panels等.
When I try to serialize the graph, the error states that FormMain.vb isn't marked for serialization. Though I've instantiated an object of StateRecord in FormMain, and FormMain calls the StateRecord.SaveState function, the StateRecord class is a separate file and is only instantiated in FormMain. Also, I do NOT try to serialize FormMain, only aspects of it like its current size and location, currently displayed Panels etc.
我不知道为什么会收到此错误,我知道Form或至少FormMain无法序列化,但是我只是序列化基本类型,例如Size,Point等. ;我感谢所有/任何建议.此外,StateRecord.SaveState的整个代码 然后出现字面上的错误消息:
I can't figure out why I'm getting this error, I'm aware that a Form, or at least FormMain isn't serializable but I'm simply serializing basic types such as Size, Point, etc. I appreciate all/any suggestions. Further, the entire code of StateRecord.SaveState and then the literal error message follow:
Public Function SaveState(Fields As ObjectModel.Collection(Of FieldType)) As Guid
'here we simply assign the passed colllection to our class-variable; "fields"
'upon this class being serialized, all statte-defining information will be stored
'
'Now serialize "Me"
Dim bf As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = Nothing
Dim graph As StateRecord = Nothing
Dim s As IO.Stream
Dim uID As Guid = Guid.NewGuid
'determine if our pathname property has been set, if not set it
If _StoragePathName = Nothing Then
_StoragePathName = GetDataDir(True) & "States\" & uID.ToString & ".gtk"
End If
'save fields to class and property
Me.ObjFields = Fields
_Fields = Fields
'set the dateSaved property to now
_DateSaved = DateTime.Now
'set our graph object to "Me"
graph = Me
'尝试序列化,返回"Nothing"(无)如果失败,则返回uID 尝试 '将流初始化为我们的存储路径名 s = IO.File.Create(_StoragePathName) '初始化我们的bineryFormatter bf =新的Runtime.Serialization.Formatters.Binary.BinaryFormatter '配置我们的BinaryFormatter,然后尝试序列化 与bf .AssemblyFormat =运行时.Serialization.Formatters.FormatterAssemblyStyle.Full .FilterLevel = Runtime.Serialization.Formatters.TypeFilterLevel.Low.序列化(图形) 结束于 异常捕获 Me.ea.ex = ex _StoragePathName =无 RaiseEvent OnError("StateRecord.SaveState()",ea) 一无所有 结束尝试 将唯一标识符设置为此类"StateRecord"; _Id = uID 返回uID 结束功能
'try the serialization, return "Nothing" if it fails, otherwise return the uID Try 'initialize the stream to our storage pathname s = IO.File.Create(_StoragePathName) 'initialize our bineryFormatter bf = New Runtime.Serialization.Formatters.Binary.BinaryFormatter 'configure our BinaryFormatter, then attempt the serialization With bf .AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full .FilterLevel = Runtime.Serialization.Formatters.TypeFilterLevel.Low .Serialize(s, graph) End With Catch ex As Exception Me.ea.ex = ex _StoragePathName = Nothing RaiseEvent OnError("StateRecord.SaveState()", ea) Return Nothing End Try 'set the unique identifier to this class "StateRecord" _Id = uID Return uID End Function
错误消息:
在程序集"GaPTrp,版本= 0.3.62242.38314,文化=中性,PublicKeyToken =空"中的类型"GaPTrp.FormMain"未标记为可序列化."
"Type 'GaPTrp.FormMain' in Assembly 'GaPTrp, Version=0.3.6242.38314, Culture=neutral, PublicKeyToken=null' is not marked as serializable."
再次感谢您的所有帮助!
Again, thanks for all help!
推荐答案
graph是StateRecord类型的对象.如果要进行序列化的代码位于Staterecord类中,则应使用"Me",因为"Me"将引用代码正在其中执行的StateRecord的实例-在您的示例中为"graph". 但是,您不是在StateRecord类中进行序列化,而是在Form1形式中进行序列化.因此,我"是指Form1,并且不可序列化.您的代码应类似于:
graph is an object of type StateRecord. If the code to do the serialization was in the class Staterecord then you would use 'Me', because 'Me' would refer to the instance of StateRecord' that the code was executing in - 'graph' in your example. But you aren't doing the serialization within the class StateRecord - you are doing it within the form Form1. Therefore 'Me' refers to Form1, and it isn't serializable. Your code should be like:
昏暗图作为新的StateRecord
graph.ObjFields =字段
bf.Serialize(s,graph)
Dim graph As New StateRecord
graph.ObjFields = Fields
bf.Serialize(s, graph)
这篇关于使用关键字"Me"的二进制序列化.失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!