2017-07-06 66 views
0

我保存在外部文件中特定對象是這樣的:如何從文件流獲取屬性的數量

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(); 
      } 

     } 

我想要什麼實現的是,如果在裝載之前,在當前的對象的改變的屬性的數量,然後我要計數通過它們在流和循環序列化對象的數量,然後將它們在當前的對象映射到那些,而不是通過循環在當前的對象屬性。這可能嗎?

回答

1

沒有明顯的方法來計算流中對象的數量。相反,你可以:

  • 序列化爲流中的第一個對象的對象的數量,或
  • 反序列化,而StreamFile.Position < StreamFile.Length

這就是說,你有一個更基本的問題。從documentationType.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); 
     } 
    } 
} 

該方案被添加至少一些針對屬性彈性,移除或重新排序。

這就是說,我不建議使用BinaryFormatter來實現對象的長期持久性。對於原因,看What are the deficiencies of the built-in BinaryFormatter based .Net serialization?相反,你可能會考慮XML,JSON或協議緩衝序列化。