避免创建不必要的SPWeb对象



http://msdn.microsoft.com/en-us/library/ff407443(v=office.14).aspx

Usethe WebsInfo property whenever possible

SPWeb对象会占用很大的内存,如果处理不当,可能会引起内存泄漏。如果要遍历一下一个网站集下面所有的站点,一种可能的办法是遍历所有的SPWeb对象,但是这样就导致了在内存里创建了很多的web对象,占用内存。

 

避免这种情况的一种办法是,避免使用SPWeb, 而是使用WebsInfoWebsInfo包含了一些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 .



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值