C# 如何从文件流中获取属性数

C# 如何从文件流中获取属性数,c#,serialization,C#,Serialization,我将特定对象保存在外部文件中,如下所示: public void SaveSettings(string SavedFilePath, object Class) { //open the stream to write the new file using (Stream StreamFile = File.Open(SavedFilePath, FileMode.CreateNew)) {

我将特定对象保存在外部文件中,如下所示:

 public void SaveSettings(string SavedFilePath, object Class)
        {

            //open the stream to write the new file
            using (Stream StreamFile = File.Open(SavedFilePath, FileMode.CreateNew))
            {
                //instantiate the binary formatter object
                BinaryFormatter binformat = new BinaryFormatter();

                //loop through all properties in input object
                foreach (PropertyInfo prop in Class.GetType().GetProperties())
                {
                    //if the property is serailizable then serialize it and write its name and value in external file
                    if (prop.PropertyType.IsSerializable)
                        binformat.Serialize(StreamFile, prop.GetValue(Class, null));

                }

                //close the stream
                StreamFile.Close();
            }
        }
 public void LoadSettings(string ConfigFile, object Class)
        {
            //open the stream to read the file
            using (Stream StreamFile = File.Open(ConfigFile, FileMode.Open))
            {
                //instantiate the binary formatter object
                BinaryFormatter binformat = new BinaryFormatter();

                //loop through all properties in input object
                foreach (PropertyInfo prop in Class.GetType().GetProperties())
                {
                    //if the property is serailizable then deserialize it and read its name and value, and write this in a memory object
                    if (prop.PropertyType.IsSerializable)
                    {
                        object objValue = binformat.Deserialize(StreamFile);
                        prop.SetValue(Class, objValue, null);
                    }
                }

                //close the stream
                StreamFile.Close();
            }

        }
然后我加载相同的对象,如下所示:

 public void SaveSettings(string SavedFilePath, object Class)
        {

            //open the stream to write the new file
            using (Stream StreamFile = File.Open(SavedFilePath, FileMode.CreateNew))
            {
                //instantiate the binary formatter object
                BinaryFormatter binformat = new BinaryFormatter();

                //loop through all properties in input object
                foreach (PropertyInfo prop in Class.GetType().GetProperties())
                {
                    //if the property is serailizable then serialize it and write its name and value in external file
                    if (prop.PropertyType.IsSerializable)
                        binformat.Serialize(StreamFile, prop.GetValue(Class, null));

                }

                //close the stream
                StreamFile.Close();
            }
        }
 public void LoadSettings(string ConfigFile, object Class)
        {
            //open the stream to read the file
            using (Stream StreamFile = File.Open(ConfigFile, FileMode.Open))
            {
                //instantiate the binary formatter object
                BinaryFormatter binformat = new BinaryFormatter();

                //loop through all properties in input object
                foreach (PropertyInfo prop in Class.GetType().GetProperties())
                {
                    //if the property is serailizable then deserialize it and read its name and value, and write this in a memory object
                    if (prop.PropertyType.IsSerializable)
                    {
                        object objValue = binformat.Deserialize(StreamFile);
                        prop.SetValue(Class, objValue, null);
                    }
                }

                //close the stream
                StreamFile.Close();
            }

        }

我想实现的是,如果在加载之前,当前对象中的属性数量发生变化,那么我想计算流中序列化对象的数量并循环它们,然后将它们映射到当前对象中的对象,而不是循环当前对象中的属性。这可能吗?

没有明显的方法来计算流中对象的数量。相反,你可以:

  • 将对象计数序列化为流中的第一个对象,或
  • StreamFile.Position
    时反序列化
也就是说,你有一个更基本的问题。从From
类型.GetProperties()

GetProperties方法不返回特定类型中的属性 顺序,如字母顺序或声明顺序您的代码不能 取决于返回属性的顺序,因为 订单不同。

您的代码完全取决于此顺序,而不是更改

或者,您可以存储属性的名称/值字典,如下所示:

public void SaveSettings(string SavedFilePath, object Class)
{
    //open the stream to write the new file
    using (Stream StreamFile = File.Open(SavedFilePath, FileMode.CreateNew))
    {
        //instantiate the binary formatter object
        BinaryFormatter binformat = new BinaryFormatter();

        //loop through all properties in input object
        var query = Class.GetType().GetProperties()
            //Make sure the property is read/write without index parameters
            .Where(p => p.GetIndexParameters().Length == 0 && p.CanRead && p.CanWrite && p.GetGetMethod() != null && p.GetSetMethod() != null)
            //if the property is serializable then serialize it and write its name and value in external file
            .Where(p => p.PropertyType.IsSerializable);

        // Create a dictionary of property names and values.
        // Note that if there are duplicate property names because a derived
        // class hides a property via a "public new" declaration, then
        // an exception will get thrown during serialization.
        var dictionary = query.ToDictionary(p => p.Name, p => p.GetValue(Class, null));

        binformat.Serialize(StreamFile, dictionary);
    }
}

public void LoadSettings(string ConfigFile, object Class)
{
    //open the stream to read the file
    using (Stream StreamFile = File.Open(ConfigFile, FileMode.Open))
    {
        //instantiate the binary formatter object
        BinaryFormatter binformat = new BinaryFormatter();

        var dictionary = (IDictionary<string, object>)binformat.Deserialize(StreamFile);

        //loop through all properties in input object
        foreach (var pair in dictionary)
        {
            var property = Class.GetType().GetProperty(pair.Key);
            if (property != null)
                property.SetValue(Class, pair.Value, null);
        }
    }
}
public void保存设置(字符串SavedFilePath,对象类)
{
//打开流以写入新文件
使用(Stream-StreamFile=File.Open(SavedFilePath,FileMode.CreateNew))
{
//实例化二进制格式化程序对象
BinaryFormatter binformat=新的BinaryFormatter();
//遍历输入对象中的所有属性
var query=Class.GetType().GetProperties()
//确保该属性在没有索引参数的情况下是读/写的
.Where(p=>p.GetIndexParameters().Length==0&&p.CanRead&&p.CanWrite&&p.GetMethod()!=null&&p.GetSetMethod()!=null)
//如果属性是可序列化的,则将其序列化并将其名称和值写入外部文件
其中(p=>p.PropertyType.IsSerializable);
//创建属性名称和值的字典。
//请注意,如果由于派生的
//类通过“public new”声明隐藏属性,然后
//序列化期间将引发异常。
var dictionary=query.ToDictionary(p=>p.Name,p=>p.GetValue(Class,null));
序列化(流文件、字典);
}
}
公共无效加载设置(字符串配置文件,对象类)
{
//打开流以读取文件
使用(Stream-StreamFile=File.Open(ConfigFile,FileMode.Open))
{
//实例化二进制格式化程序对象
BinaryFormatter binformat=新的BinaryFormatter();
var dictionary=(IDictionary)binformat.Deserialize(StreamFile);
//遍历输入对象中的所有属性
foreach(字典中的var对)
{
var property=Class.GetType().GetProperty(pair.Key);
if(属性!=null)
属性.SetValue(类,对.Value,null);
}
}
}
该方案至少对添加、删除或重新排序的属性具有一定的弹性

话虽如此,我不建议使用
BinaryFormatter
长期持久化对象。因此,您可以考虑XML、JSON或协议缓冲区序列化。