Reshaper为什么认为此代码不可用

问题描述 投票:0回答:1

[在编码时,我注意到ReSharper(v2019.1.1)认为我的部分代码无法访问:

Unreachable Code

我已经学会了信任ReSharper,因此我建立了一个测试工具以查看发生了什么:

void Main()
{
    List<string> scopeList = new List<string>();
    Dictionary<string, string> customFields = new Dictionary<string, string>();

    var mainScope = new Scope();
    var subScope = new Scope();
    subScope.Parent = mainScope;

    dynamic currentState = new Tuple<String, int>("bob", 1565);
    mainScope.State = currentState;
    subScope.State = "Hello";

    WriteScopes(subScope, scopeList, customFields);

    Console.WriteLine(customFields);
    Console.WriteLine(scopeList);
}

public void WriteScopes(Scope scope, List<string> scopeList, Dictionary<string, string> customFields)
{
    var loopScope = scope;

    while (loopScope != null)
    {
        var loopState = loopScope.State;
        if (loopState == null) continue;
        if (loopState is (string name, var value))
        {
            customFields.Add(name, value.ToString());
        }
        else
        {
            scopeList.Add(loopState.ToString());
        }

        loopScope = loopScope.Parent;
    }
}

public class Scope
{
    public Scope Parent { get; set; }
    public object State { get; set; }
}

[运行测试时,似乎"Hello"字符串正在执行”无法访问的代码”。但是如上所述,我已经学会信任ReSharper,所以我想知道发生了什么事?

ReSharper为什么认为代码无法访问?

c# .net-core resharper
1个回答
0
投票

所以,我完全不知道为什么代码被标记为不可访问,但是在ReSharper 2019.3.4的my VS中,我没有看到此消息。这可能是该工具中的错误。

© www.soinside.com 2019 - 2024. All rights reserved.