C# 序列化CustomType的泛型列表不会序列化元素';变量

C# 序列化CustomType的泛型列表不会序列化元素';变量,c#,serialization,C#,Serialization,这是我的数据类: public class Data { [Serializable] public List<SomeType> myList = new List<SomeType>(20); private string dataPath = "SomePath"; void Save() { using(FileStream fs = File.Create(dataPath)) {

这是我的数据类:

public class Data
{
    [Serializable]
    public List<SomeType> myList = new List<SomeType>(20);
    private string dataPath = "SomePath";

    void Save()
    {
        using(FileStream fs = File.Create(dataPath))
        {
            BinaryFormatter formatter = new BinaryFormatter()
            formatter.Serialize(fs, myList);
        }
    }

    void Load()
    {
        using(FileStream fs = File.Open(dataPath, FileMode.Open))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            myList = (List<SomeType>) formatter.Deserialize(fs);
        }
    }
}
我意识到,当我加载文件时,所有元素的isUsed变量都会变为false,尽管我确信在运行时大多数元素都会变为true。我做错了什么?它是否与isUsed不是属性有关?

这应该可以:

    [Serializable]
    public class SomeType
    {
        public bool isUsed;
    }

    public class Data
    {
        //removed because its not permitted here,you already have sometype serializable...
        public List<SomeType> myList = new List<SomeType>(20);
        private string dataPath = "SomePath";

        public void Save()
        {
            using (FileStream fs = File.Create(dataPath))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fs, myList);

            }
            //just for testing purposes i cleared the list at this point...
            myList.Clear();
        }

        public void Load()
        {
            using (FileStream fs = File.Open(dataPath, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                myList = (List<SomeType>)formatter.Deserialize(fs);
            }
        }
    }
即使它不存在,也将根据filemode.append创建它


您应该将流传递给serialize方法,而且像myList这样的公共字段也不是一件好事,您至少可以为它创建一个属性。

在保存函数中,您似乎正在创建一个文件流,但没有使用它。您可能无法编译。您没有向格式化程序传递任何流。序列化,但它应该创建的文件似乎不存在。@YuvalItzchakov什么意思?哦,对不起,只是我忘了在这里键入它。我实际上是在实际代码中传递fs来序列化函数。我会马上纠正的。对不起,除了您删除的不必要的属性之外,我没有注意到任何区别。这一行在save方法-formatter.Serialize(fs,myList);。属性不是不必要的,它在那里是无效的…是的,我很抱歉我忘了将fs传递给我的Serialize方法。如上所述,这只是我在这里犯的一个错误。我修复了它。请提供初始化类、填充列表以及保存和加载操作的代码。
    [Serializable]
    public class SomeType
    {
        public bool isUsed;
    }

    public class Data
    {
        //removed because its not permitted here,you already have sometype serializable...
        public List<SomeType> myList = new List<SomeType>(20);
        private string dataPath = "SomePath";

        public void Save()
        {
            using (FileStream fs = File.Create(dataPath))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fs, myList);

            }
            //just for testing purposes i cleared the list at this point...
            myList.Clear();
        }

        public void Load()
        {
            using (FileStream fs = File.Open(dataPath, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                myList = (List<SomeType>)formatter.Deserialize(fs);
            }
        }
    }
File.Open(dataPath,FileMode.Append,FileAccess.Write)