http://msdn.microsoft.com/en-us/library/ff407443(v=office.14).aspx
Usethe WebsInfo property whenever possible
SPWeb对象会占用很大的内存,如果处理不当,可能会引起内存泄漏。如果要遍历一下一个网站集下面所有的站点,一种可能的办法是遍历所有的SPWeb对象,但是这样就导致了在内存里创建了很多的web对象,占用内存。
避免这种情况的一种办法是,避免使用SPWeb, 而是使用WebsInfo。WebsInfo包含了一些web的基本信息,包括:
- Configuration
- CustomMasterUrl
- Description
- Id
- Language
- LastItemModifiedDate
- MasterUrl
- ServerRelativeUrl
- Title
- UIVersion
- UIVersionConfigurationEnabled
- WebTemplateId
下面的代码,演示了如何使用WebsInfo对象,来避免生成SPWeb对象。
SPSite site = SPContext.Current.Site;
SPWebCollection subWebs = site.AllWebs;
foreach (SPWebInfo webInfo insubWebs.WebsInfo)
{
//Create a result object that containsinformation about each SPWeb in the collection.
var result = webInfo.Title +
webInfo.ServerRelativeUrl+
webInfo.Description+
webInfo.Id+
webInfo.Language+
webInfo.LastItemModifiedDate+
webInfo.WebTemplateId+
webInfo.Configuration+
webInfo.UIVersionConfigurationEnabled+
webInfo.UIVersion+
webInfo.MasterUrl+
webInfo.CustomMasterUrl;
}
如果你想知道这种方法道题能提升多少运行效率,请参考 The SPWebCollection.WebsInfo property .