问题描述
我在我的C#ASP.Net程序被回传时填写对象的数组,我希望下一次回传时恢复它们。为此我在默认的类定义的数组声明是这样保存的: -
I have in my C# ASP.Net program an array of objects that are filled in during a postback and I wish to recover them during the next postback. To this end I include the array declaration in the Default class definition which is saved thus:-
this.Session["This"] = this;
和恢复: -
Default saved_stuff = (Default) this.Session["This"];
这一切工作正常,除了: -
This works fine for everything apart from:-
MyClass [] my_array;
在恢复, saved_stuff.my_array
总是空
。
MyClass的定义是这样的: -
MyClass is defined like this:-
public MyClass : ISerializable
{
private string some_string;
private double some_double;
// some more simple members
// Some getters and setters
// ISerializable Implementation
}
我曾试图使 MyClass的
实施 ISerializable的
但这并没有什么差别。有谁知道我应该做的?
I have tried making MyClass
implement ISerializable
but that doesn't make any difference. Does anyone know what I should be doing?
修改回答@迈克尔的问题,那么我在做这样的事情...
Edit to answer @Michael's question, I'm then doing things like...
for (int i = 0; i <= saved_stuff.my_array.GetUpperBound(0); i++)
{
// blah blah
}
这是与对象引用不设置为一个对象的一个实例失败。所有默认
的其他成员变量是可见的 saved_stuff
时调试。
which is failing with "object reference is not set to an instance of an object". All the other member variables of Default
are visible in saved_stuff
when in debug.
推荐答案
您必须存储在它自己的会话对象的数组数据:
You have to store your array data in its own session object:
MyClass[] my_array = //Something
Session["ArrayData"] = my_array;
然后检索为:
var arrayData = (MyClass[])Session["ArrayData"];
这篇关于如何保存对象在C#ASP.Net Sesssion一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!