为什么 Resharper 认为此代码无法访问
Why does Resharper Think This Code is Unreachable
在编码时,我注意到 ReSharper (v2019.1.1) 认为我的部分代码无法访问:
我学会了信任 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; }
}
当我 运行 我的测试时,似乎 "unreachable code" 正在被 "Hello"
字符串执行。但正如我上面所说,我已经学会信任 ReSharper,所以我想知道这是怎么回事?
为什么 ReSharper 认为代码无法访问?
嗯,我确实不知道为什么代码被标记为无法访问,但在 my VS 和 ReSharper 2019.3.4 中,我没有看到消息。这可能是工具中的错误。
在编码时,我注意到 ReSharper (v2019.1.1) 认为我的部分代码无法访问:
我学会了信任 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; }
}
当我 运行 我的测试时,似乎 "unreachable code" 正在被 "Hello"
字符串执行。但正如我上面所说,我已经学会信任 ReSharper,所以我想知道这是怎么回事?
为什么 ReSharper 认为代码无法访问?
嗯,我确实不知道为什么代码被标记为无法访问,但在 my VS 和 ReSharper 2019.3.4 中,我没有看到消息。这可能是工具中的错误。