您的位置:首页 > 其它

关于对象和引用的误区。

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等等,用能不能保存数据了???????就像单机版的那样子
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: