我们有代码有时我们会从函数返回SPWEB对象。因此:例如:

public SPWeb getDeptWeb()
{
    SPWeb deptWeb = SpSite.OpenWeb(SpContext.Web.ID);
    ...
    return deptWeb;
}

在这种情况下,我们如何处理SPWEB对象?还是在我们接受返回的参数的地方处置它是否足够?

有帮助吗?

解决方案

最好的方法可能是处置 SPWeb 在呼叫者中,例如 使用 陈述:

public SPWeb getDeptWeb()
{
    SPWeb deptWeb = SpSite.OpenWeb(SpContext.Web.ID);
    // ...
    return deptWeb;
}

public void Foo()
{
    using (SPWeb deptWeb = getDeptWeb()) {
        // Do something with the website...
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top