可以序列化具有静态构造函数的类吗?
本文关键字:构造函数 静态 序列化 | 更新日期: 2023-09-27 18:05:20
我有以下要序列化的类。是否可以在二进制序列化器中序列化?我在MSDN上尝试过下面的帖子https://social.msdn.microsoft.com/forums/vstudio/en us/9e8d37f0 - 8 df2 - 45 - d9 - 9902 - 6 - ab343371c2d/how - -我做- xml序列化- -类的静态属性-在c?forum=csharpgeneral
但出现以下错误
错误1可访问性不一致:属性类型"System.Collections.Generic。List'则不太容易访问> property 'kafka01.HAND。_HH"c: ' '超高速' '视觉资料studio 2013'Projects'kafka01'kafka01'HAND.cs 15 32 kafka01
[Serializable()]
public class HAND : ISerializable
{
public static List<HH> _HH { get; private set; }
static HAND()
{
_HH = new List<HH>()
{
new HH(), new HH(), new HH(),
new HH(), new HH(), new HH()
};
}
internal class HH
{
public string hand { get; set; }
public string hand_sort { get; set; }
public string board { get; set; }
public int board_length { get; set; }
public bool transformed { get; set; }
public int z { get; set; }
public int bb { get; set; }
public double eff { get; set; }
public double s_us { get; set; }
public double s_vil { get; set; }
public double bet_us { get; set; }
public double bet_vil { get; set; }
public double blind_us { get; set; }
public double blind_vil { get; set; }
public int pos { get; set; }
public string pre { get; set; }
public bool pre_set { get; set; }
public bool new_pre { get; set; }
public bool image_processing_lock { get; set; }
public string node_action { get; set; }
public string pre_action { get; set; }
public string last_action { get; set; }
public bool action_switch { get; set; }
public bool action_switch1 { get; set; }
}
//Deserialization constructor.
public HAND(SerializationInfo info, StreamingContext ctxt)
{
_HH = (List<HH>)info.GetValue("innerClass", typeof(List<HH>));
//Get the values from info and assign them to the appropriate properties
}
//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("innerClass", _HH);
//You can use any custom name for your name-value pair. But make sure you
// read the values with the same name. For ex:- If you write _HH as "innerClass"
// then you should read the same with "innerClass"
//
}
}
这是我的主要方法
HAND h = new HAND();
HAND._HH[0].board = "my val";
FileStream fileStream = new FileStream("test.binary", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fileStream, h);
fileStream.Dispose();
BinaryDeserialize();
Console.ReadLine();
这是我的BinaryDeserialize方法
> private static void BinaryDeserialize()
> {
> FileStream fileStream = new FileStream("test.binary", FileMode.Open);
> BinaryFormatter bf = new BinaryFormatter();
> HAND myClass = (HAND)bf.Deserialize(fileStream);
> Console.WriteLine(HAND._HH[0].board.ToString());
> //MessageBox.Show(MyClass.StartIndex.HasValue.ToString());
> // MessageBox.Show(MyClass.StartIndex.Value.ToString());
> }
回答您的问题:是的,重要的是要注意,当对象被反序列化时不会调用构造函数。
修复您的问题:使HH类public
和使List<HH> _HH { get; public set; }
属性设置公共
1-将"HH"类设置为public
2-将"Hand"类设为public
再次编译,错误应该消失