的SPListItemCollection是否可以重新打开SPW

的SPListItemCollection是否可以重新打开SPW

本文介绍了使用从函数返回的SPListItemCollection是否可以重新打开SPWeb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 Stefan之后戈斯纳(Gossner)关于处理对象的帖子以及关于交叉方法处理模式的问题 >,我发现我因意外重新打开某些SPWeb而感到内gui.我知道在Stefan Gossner的帖子中,他提到您在处理完所有子对象之后都应该处置SPWeb.但是, Microsoft文档提到了缓存SPListItemCollection对象.以下代码正确吗?返回的SPListItemCollection是否会重新打开SPWeb对象?有什么办法可以确定吗?

After reading Stefan Gossner's post about disposing objects and this question about Cross method dispose patterns, I found that I was guilty of accidentally reopening some SPWebs. I know in Stefan Gossner's post he mentions you should dispose of an SPWeb after you are finished with any child object. However, the microsoft documentation mentions Caching the SPListItemCollection object. Is the following code correct? Would the returned SPListItemCollection reopen an SPWeb object? Is there any way to tell for sure?

// is this correct????
private SPListItemCollection GetListItems()
{
    SPListItemCollection items = null;
    try
    {
        using (SPSite site = new SPSite(GetListSiteUrl()))
        {
            using (SPWeb web = site.OpenWeb())
            {
                // retrieve the list
                SPList list = web.Lists[_ListName];

                // more code to create the query...
                items = list.GetItems(query);
            }
        }
    }
    catch (Exception e)
    {
        // log error
    }
    return items;
}

编辑09/09/09

我主要指的是 Stefan Grossner的帖子:

我相信他在说的是,如果我在处置用来获取SPWeb的SPWeb之后使用SPListItemCollection,则会自动重新打开SPWeb.

I believe what he is saying is that if I use the SPListItemCollection after I dispose of the SPWeb that I used to get it... the SPWeb will be reopened automatically.

推荐答案

我在直接问Stefan SPListItemCollection确实可以在销毁SPWeb之后重新打开它.这意味着我上面发布的代码是错误的,并且只有在使用SPListItemCollection之后才能处置SPWeb.

I found out after asking Stefan directly that the SPListItemCollection can indeed reopen the SPWeb after you dispose of it. This means that my code posted above is INCORRECT and I would only be able to dispose of the SPWeb after I use the SPListItemCollection.

更新:最好将SPListItemCollection转换为其他内容,然后将其返回.

Update: It is better to convert to the SPListItemCollection to something else and return that instead.

private DataTable GetListItems()
{
    DataTable table = null;
    try
    {
        SPListItemCollection items = null;
        using (SPSite site = new SPSite(GetListSiteUrl()))
        {
            using (SPWeb web = site.OpenWeb())
            {
                // retrieve the list
                SPList list = web.Lists[_ListName];

                // more code to create the query...
                items = list.GetItems(query);

                // convert to a regular DataTable
                table = items.GetDataTable();
            }
        }
    }
    catch (Exception e)
    {
        // log error
    }
    return table;
}

这篇关于使用从函数返回的SPListItemCollection是否可以重新打开SPWeb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:27