我保存在外部文件中特定對象是這樣的:如何從文件流獲取屬性的數量
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();
}
}
我想要什麼實現的是,如果在裝載之前,在當前的對象的改變的屬性的數量,然後我要計數通過它們在流和循環序列化對象的數量,然後將它們在當前的對象映射到那些,而不是通過循環在當前的對象屬性。這可能嗎?