关于对象和引用的误区。
2006-10-13 23:37
309 查看
今天我被一个理解折磨了半天。 在PortalCSVS中发现了个东西。
代码如下
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items["SiteSettings"];//
moduleRow.ModuleOrder = moduleOrder;
moduleRow.ModuleTitle = title;
moduleRow.PaneName = paneName;
moduleRow.CacheTimeout = cacheTime;
moduleRow.EditRoles = editRoles;
moduleRow.ShowMobile = showMobile;
// Save the changes
SaveSiteSettings();//此函数一用,cache就没有了。
而HttpContext.Current.Items["SiteSettings"]的初始化是来源于GetSiteSettings();
HttpContext.Current.Items.Add("SiteSettings", Configuration.GetSiteSettings());
而cache来源于一个临时的SiteConfiguration
public static SiteConfiguration GetSiteSettings()
{
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Cache["SiteSettings"];
// If the SiteConfiguration isn't cached, load it from the XML file and add it into the cache.
if(siteSettings == null)
{
// Create the dataset
siteSettings = new SiteConfiguration();
// Retrieve the location of the XML configuration file
string configFile = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["configFile"]);
// Set the AutoIncrement property to true for easier adding of rows
siteSettings.Tab.TabIdColumn.AutoIncrement = true;
siteSettings.Module.ModuleIdColumn.AutoIncrement = true;
siteSettings.ModuleDefinition.ModuleDefIdColumn.AutoIncrement = true;
// Load the XML data into the DataSet
siteSettings.ReadXml(configFile);
// Store the dataset in the cache
HttpContext.Current.Cache.Insert("SiteSettings", siteSettings, new CacheDependency(configFile));
}
return siteSettings;
/**///////////////////////////////////////////////////////////////
...{ ArrayList al = new ArrayList();
al.Add("afafa");
HttpContext.Current.Cache.Insert("bbbb", al);
ArrayList cach = (ArrayList)HttpContext.Current.Cache["bbbb"];
HttpContext.Current.Items.Add("aa", cach);
ArrayList Item = (ArrayList)HttpContext.Current.Cache["bbbb"];//(ArrayList)HttpContext.Current.Items["aa"];
cach = null;
//al.Clear();
this.Response.Write("Items是:"+Item.Count.ToString()+"<br>");
}
//this.Response.Write("cach是:" + cach.Count.ToString() + "<br>");
//输出1, cach 相当一个指针,只是不指向地址了,并不是说地址没了,要注意了。
/**///////////////////////////////////////////////////////////////
...{ArrayList al = new ArrayList();
al.Add("afafa");
HttpContext.Current.Cache.Insert("bbbb", al);
ArrayList cach = (ArrayList)HttpContext.Current.Cache["bbbb"];
cach = null;
HttpContext.Current.Items.Add("aa", cach);
ArrayList Item = (ArrayList)HttpContext.Current.Cache["bbbb"];//(ArrayList)HttpContext.Current.Items["aa"];
al.Clear();
}
this.Response.Write(HttpContext.Current.Cache["bbbb"].GetType().ToString()+"<br>Items是:"+Item.Count.ToString()+"<br>");
//输出0
/**///////////////////////////////////////////////////////////////
...{ ArrayList al = new ArrayList();
al.Add("afafa");
HttpContext.Current.Cache.Insert("bbbb", al);
ArrayList cach = (ArrayList)HttpContext.Current.Cache["bbbb"];
//cach = null;
HttpContext.Current.Items.Add("aa", cach);
ArrayList Item = (ArrayList)HttpContext.Current.Items["aa"];//(ArrayList)HttpContext.Current.Items["aa"];
cach.Clear();
this.Response.Write(HttpContext.Current.Cache["bbbb"].GetType().ToString()+"<br>Items是:"+Item.Count.ToString()+"<br>");
//输出0
}
/**///////////////////////////////////////////////////////////////
...{ ArrayList al = new ArrayList();
al.Add("afafa");
HttpContext.Current.Cache.Insert("1", al);
HttpContext.Current.Items.Add("1", HttpContext.Current.Cache["1"]);
HttpContext.Current.Cache.Remove("1");
ArrayList Item = HttpContext.Current.Items["1"] as ArrayList;//(ArrayList)HttpContext.Current.Items["aa"];
// cach.Clear();
this.Response.Write("<br>Items是:"+Item.Count.ToString()+"<br>");
}
//输出1
可见,item可以直接依赖Cache,Item只是拷贝真正对像的地址,Cache空了后,item引用不为空,仍然指向原来的对象。
另一个问题,我不用cache等等,用能不能保存数据了???????就像单机版的那样子
代码如下
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items["SiteSettings"];//
moduleRow.ModuleOrder = moduleOrder;
moduleRow.ModuleTitle = title;
moduleRow.PaneName = paneName;
moduleRow.CacheTimeout = cacheTime;
moduleRow.EditRoles = editRoles;
moduleRow.ShowMobile = showMobile;
// Save the changes
SaveSiteSettings();//此函数一用,cache就没有了。
而HttpContext.Current.Items["SiteSettings"]的初始化是来源于GetSiteSettings();
HttpContext.Current.Items.Add("SiteSettings", Configuration.GetSiteSettings());
而cache来源于一个临时的SiteConfiguration
public static SiteConfiguration GetSiteSettings()
{
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Cache["SiteSettings"];
// If the SiteConfiguration isn't cached, load it from the XML file and add it into the cache.
if(siteSettings == null)
{
// Create the dataset
siteSettings = new SiteConfiguration();
// Retrieve the location of the XML configuration file
string configFile = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["configFile"]);
// Set the AutoIncrement property to true for easier adding of rows
siteSettings.Tab.TabIdColumn.AutoIncrement = true;
siteSettings.Module.ModuleIdColumn.AutoIncrement = true;
siteSettings.ModuleDefinition.ModuleDefIdColumn.AutoIncrement = true;
// Load the XML data into the DataSet
siteSettings.ReadXml(configFile);
// Store the dataset in the cache
HttpContext.Current.Cache.Insert("SiteSettings", siteSettings, new CacheDependency(configFile));
}
return siteSettings;
/**///////////////////////////////////////////////////////////////
...{ ArrayList al = new ArrayList();
al.Add("afafa");
HttpContext.Current.Cache.Insert("bbbb", al);
ArrayList cach = (ArrayList)HttpContext.Current.Cache["bbbb"];
HttpContext.Current.Items.Add("aa", cach);
ArrayList Item = (ArrayList)HttpContext.Current.Cache["bbbb"];//(ArrayList)HttpContext.Current.Items["aa"];
cach = null;
//al.Clear();
this.Response.Write("Items是:"+Item.Count.ToString()+"<br>");
}
//this.Response.Write("cach是:" + cach.Count.ToString() + "<br>");
//输出1, cach 相当一个指针,只是不指向地址了,并不是说地址没了,要注意了。
/**///////////////////////////////////////////////////////////////
...{ArrayList al = new ArrayList();
al.Add("afafa");
HttpContext.Current.Cache.Insert("bbbb", al);
ArrayList cach = (ArrayList)HttpContext.Current.Cache["bbbb"];
cach = null;
HttpContext.Current.Items.Add("aa", cach);
ArrayList Item = (ArrayList)HttpContext.Current.Cache["bbbb"];//(ArrayList)HttpContext.Current.Items["aa"];
al.Clear();
}
this.Response.Write(HttpContext.Current.Cache["bbbb"].GetType().ToString()+"<br>Items是:"+Item.Count.ToString()+"<br>");
//输出0
/**///////////////////////////////////////////////////////////////
...{ ArrayList al = new ArrayList();
al.Add("afafa");
HttpContext.Current.Cache.Insert("bbbb", al);
ArrayList cach = (ArrayList)HttpContext.Current.Cache["bbbb"];
//cach = null;
HttpContext.Current.Items.Add("aa", cach);
ArrayList Item = (ArrayList)HttpContext.Current.Items["aa"];//(ArrayList)HttpContext.Current.Items["aa"];
cach.Clear();
this.Response.Write(HttpContext.Current.Cache["bbbb"].GetType().ToString()+"<br>Items是:"+Item.Count.ToString()+"<br>");
//输出0
}
/**///////////////////////////////////////////////////////////////
...{ ArrayList al = new ArrayList();
al.Add("afafa");
HttpContext.Current.Cache.Insert("1", al);
HttpContext.Current.Items.Add("1", HttpContext.Current.Cache["1"]);
HttpContext.Current.Cache.Remove("1");
ArrayList Item = HttpContext.Current.Items["1"] as ArrayList;//(ArrayList)HttpContext.Current.Items["aa"];
// cach.Clear();
this.Response.Write("<br>Items是:"+Item.Count.ToString()+"<br>");
}
//输出1
可见,item可以直接依赖Cache,Item只是拷贝真正对像的地址,Cache空了后,item引用不为空,仍然指向原来的对象。
另一个问题,我不用cache等等,用能不能保存数据了???????就像单机版的那样子
相关文章推荐
- 由无名对象(临时对象)引发的关于“引用”的思考
- 关于java中对象的循环引用问题
- C# 关于“未将对象引用设置到对象的实例。”错误
- 关于“System.NullReferenceException: 未将对象引用设置到对象的实例”问题原因。
- 关于解决JavaScript中使用require引用外部js时出现的某个对象 not defined 时现时消失 问题
- 关于父类引用指向子类对象
- 关于java基础中,接口里面父类的对象指向子类的引用
- ASP.NET关于"未将对象引用设置到对象的实例"异常的原因
- 关于Fragment.onCreate()中引用Activity.onCreate()创建的对象时的一个异常
- Java 关于多态中,父类引用子类对象中的“引用”解释,释义
- 关于引用对象的使用的一点小理解
- 关于函数返回引用和const对象
- 关于URLRewriter报错:System.NullReferenceException: 未将对象引用设置到对象的实例 的解决
- Java关于父类引用指向子类对象
- 关于js中一个对象当做参数传递是按值传递还是按引用传递的个人看法
- 关于对象与引用之间的一些基本概念
- 关于对象引用
- 关于对象与引用之间的一些基本概念。
- 关于java中对象的循环引用问题
- 关于与单例与采用静态变量引用一个对象的区别