【问题标题】:unreachable code detected in a foreach在 foreach 中检测到无法访问的代码
【发布时间】:2013-06-26 13:02:14
【问题描述】:

我在 vs2012 中收到此警告,但无法弄清楚 无法访问的代码在哪里。

private void LoadGridData()
{
    string currentUrl = SPContext.Current.Site.Url;

    var jobInfoList = new List<JobInfo>();

    SPSecurity.RunWithElevatedPrivileges(delegate
        {
            using (var clientSiteCollection = new SPSite(currentUrl))
            {
                foreach (
                    SPWeb web in
                        clientSiteCollection.AllWebs.Where(
                            c =>
                            c.AllProperties[Constants.WebProperties.General.WebTemplate] != null &&
                            c.AllProperties[Constants.WebProperties.General.WebTemplate].ToString() ==
                            Constants.WebTemplates.JobWebPropertyName).OrderByDescending(d => d.Created).Take(5)
                    )
                {
                    SPList jobInfoListSp = web.Lists.TryGetList(Constants.Lists.JobInfoName);
                    if (jobInfoListSp != null)
                    {
                        if (jobInfoListSp.Items.Count > 0)
                        {
                            var value =
                                new SPFieldUrlValue(
                                    jobInfoListSp.Items[0][Constants.FieldNames.Job.Link].ToString());

                            jobInfoList.Add(new JobInfo
                                {
                                    JobName =
                                        jobInfoListSp.Items[0][Constants.FieldNames.Job.JobName].ToString(),
                                    JobCode =
                                        jobInfoListSp.Items[0][Constants.FieldNames.Job.JobCode].ToString(),
                                   Link = value.Url,
                                    JobWebsite = web.Url,
                                    IsConfidential =
                                        Convert.ToBoolean(
                                            jobInfoListSp.Items[0][Constants.FieldNames.Job.Confidential])
                                });
                        }
                    }

                    web.Dispose();
                }
            }
        });

    _lastCreatedJobsGrid.DataSource = jobInfoList;
    _lastCreatedJobsGrid.DataBind();
}

【问题讨论】:

  • 您是否有任何高于此代码的代码永远不会让此代码执行?
  • 或实际循环中的任何代码?
  • 哪一行不可达?你能想出一个简短但完整的例子来说明这个问题吗?
  • 根据 VS,无法访问的 LINE 是 foreach 本身,这就是为什么它对我来说很奇怪。我更新了代码以包含所有方法。
  • 对不赞成票的人感到羞耻。

标签: c# sharepoint code-analysis fxcop


【解决方案1】:

愚蠢的回答大声笑,其中一个常量被移动了,然后类不再编译,但是警告在那里,一旦我修复了常量引用,警告就消失了。

【讨论】:

    猜你喜欢
    • 2011-05-09
    • 2018-09-21
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多