我现在有点不知所措。我正在尝试创建一个MVC项目,我在其中显示项目,有点像目录。 我希望能够将项目保存在某种IEnumerable集合中,然后将其保存到文件中。 然后,我希望能够从文件中提取数据,并使用保存文件中的项目重新填充IEnumerable集合。
但是,我遇到了一个问题,我无法从保存文件中取回任何项目。
以下是我的保存和加载方法:
public void SaveProducts(IEnumerable<Product> col)
{
Stream stream = File.Open("C:\\temp\\Products.txt", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, col);
stream.Close();
}
public IEnumerable<Product> GetProducts()
{
IEnumerable<Product> products;
Stream stream = File.Open("C:\\temp\\Products.txt", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
products = (IEnumerable<Product>)bformatter.Deserialize(stream);
stream.Close();
return products;
}
然后我尝试将信息拉回来:
IEnumerable<Product> retrievedProducts = dal.GetProducts();
this.products = retrievedProducts.ToList();
foreach (Product p in products)
{
Console.WriteLine(p.Name);
}
dal.SaveProducts(products);
我不太清楚从文件中拉回项目的原因是什么。我的Console.WriteLine从不打印任何内容,因此似乎没有从文件本身检索任何内容。
修改
我的产品类:
[Serializable()]
public class Product : ISerializable
{
public static int nextId = 1;
public int Id { get; set; }
[Required(ErrorMessage = "Please enter a name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter a description")]
public string Description { get; set; }
[Required(ErrorMessage = "Please enter a price")]
public float ListPrice { get; set; }
public Product()
{
Id = nextId++;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Id", Id);
info.AddValue("Name", Name);
info.AddValue("Desc", Description);
info.AddValue("Price", ListPrice);
}
public Product(SerializationInfo info, StreamingContext context)
{
Id = (int)info.GetValue("Id", typeof(int));
Name = (string)info.GetValue("Name", typeof(string));
Description = (string)info.GetValue("Desc", typeof(string));
ListPrice = (float)info.GetValue("Price", typeof(float));
}
}
答案 0 :(得分:0)
尝试在调用序列化之前添加ToList():
bformatter.Serialize(stream,col.ToList());
你确定当你调用dal.GetProducts()时你真的有文件中的数据吗?换句话说,在调用GetProducts()方法之前,您是否在某处调用dal.SaveProducts(products)?
我已经使用以下代码测试了您的代码,它正在运行......
var products = new List<Product>();
products.Add(new Product() { Name = "product1" });
products.Add(new Product() { Name = "product2" });
dal.SaveProducts(products);
var data = dal.GetProducts();
答案 1 :(得分:0)
我试图重现你的问题,但对我来说它有效:
static void Main(string[] args)
{
DAL dal = new DAL();
var products = dal.GetInitialProducts().ToList();
foreach (Product p in products)
{
Console.WriteLine("{0} {1} {2} {3}", p.Name, p.Id, p.Description, p.ListPrice);
}
dal.SaveProducts(products);
IEnumerable<Product> retrievedProducts = dal.GetProducts();
Console.WriteLine("\nRetrieved Products");
foreach (Product p in retrievedProducts)
{
Console.WriteLine("{0} {1} {2} {3}", p.Name, p.Id, p.Description, p.ListPrice);
}
Console.ReadLine();
}
我在DAL对象中添加了以下方法:
public IEnumerable<Product> GetInitialProducts()
{
List<Product> list = new List<Product>
{
new Product{Name = "First", Description = "descr1", Id = 1, ListPrice = 1.1F},
new Product{Name = "Second", Description = "descr2", Id = 2, ListPrice = 2.2F},
};
return list;
}