使用Accord.Net,我尝试序列化/反序列化Codification类的实例,但在尝试反序列化时收到Null Reference Exception。序列化对象时我没有遇到任何问题。如果实例没有初始化列,则不会抛出异常。
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=Accord
StackTrace:
at Accord.Collections.TwoWayDictionary`2.OnDeserialized(StreamingContext context)
at System.Runtime.Serialization.SerializationEventHandler.Invoke(StreamingContext context)
at System.Runtime.Serialization.ObjectManager.RaiseDeserializationEvent()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
我使用了NaiveBayes中的示例表来测试
DataTable table = new DataTable("Mitchell's Tennis Example");
table.Columns.AddRange(new DataColumn[]{
new DataColumn("Day", typeof(string)),
new DataColumn("Outlook", typeof(string)),
new DataColumn("Temperature", typeof(string)),
new DataColumn("Humidity", typeof(string)),
new DataColumn("Wind", typeof(string)),
new DataColumn("PlayTennis", typeof(string))});
table.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
table.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No");
table.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes");
table.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes");
table.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes");
table.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No");
table.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes");
table.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No");
table.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes");
table.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes");
table.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes");
table.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes");
table.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes");
table.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No");
Codification codebook = new Codification(table);
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, codebook);
ms.Seek(0, SeekOrigin.Begin);
object test = bf.Deserialize(ms); //Exception thrown here
}