在C#项目中工作时,Visual Studio 15具有Find All References
的快捷方式,它将在'...' references
面板中显示在整个解决方案中使用的变量的所有引用。
是否可以仅查看变量分配到的Find All References
结果的子集?
鉴于此人为设计的Foo.cs
:
public class Bar
{
private int val;
public Bar(int val) { this.val = val; }
public SetValue(int val) { this.val = val; }
}
public class Foo
{
public Bar bar = new Bar(111);
public Foo()
{
bar = new Bar(222);
bar.SetValue(333);
Modify(ref bar, 444);
Set(out bar, 555);
}
public void Modify(ref Bar toModify, int val)
{
toModify.SetValue(val);
}
public void Set(out Bar toSet, int val)
{
toSet = new Bar(val);
}
}
如果我将光标放在Foo.bar
的声明上并按Shift+F12
的{{1}}(或右键单击并选择相同的选项),则会得到以下4行:
我想要一种简便的方法,只是获取引用的子集,该子集专门包含对Find All References
的保证分配。
理想情况下,我希望有一个类似于bar
的快捷方式来执行这样的操作,例如Shift+F12
。
特别是在执行时,我希望Ctrl+Shift+F12
结果窗口能够:
Find All References
(忽略初始定义)bar = new Bar(222)
参数传递,可以保证为其分配值。out
ref
的其他任何引用。在此示例中,我只想查看带有值bar
和222
的行,因为它们是唯一的保证分配:
我知道555
结果窗口的右上方有一个搜索栏,我可以用它来过滤Find All References
返回的现有引用集,因此可以过滤Shift+F12
(必须用=
转义)和\
:
但这并不理想
out
和=
的操作。out
要重申:是否可以以类似于 // False positive, 'bar' not assigned but line contains '='
var derp = bar.GetValue();
if(bar == null) {...}
bar.SetString("===");
// False negative,'bar' assigned but line does not contain '='
Bar
= new Bar(666);
的方式执行基本语法分析,但是仅返回对相关变量执行有保证赋值的引用?
目标是在大型项目中使用此功能,在大型项目中,给定变量的引用有成千上万个,但只有少数分配,并且能够具有某种机制以便快速查看这些特定的代码行。
如果有一种方法可以添加键盘快捷键,使其仅执行Find All References
并在搜索栏中自动填充Find All References
,但这是可接受的,但由于不理想如上所述的假阳性/阴性。由于误报不太可能发生,因此至少可以将引用范围缩小到较小的子集。
一种理想的解决方案是利用Visual Studio的代码知识来定位所有在语法上分配给定变量的实例。
答案 0 :(得分:0)
这是VisualStudio's UserVoice page上要求的功能
在挖掘DotNet/Roslyn Github repo时,有一个问题讨论了我要寻找的确切功能:https://github.com/dotnet/roslyn/issues/22545
特别是能够通过什么样的引用来过滤function JSON_to_buffer(json) {
let buff = Buffer.from(json);
if ( json.length < constants.min_draft_size_for_compression) {
return buff;
}
return zlib.deflate(buff, (err, buffer) => {
if (!err) {
return buffer;
} else {
return BPromise.reject(new VError({
name: 'BufferError',
}, err));
}
});
}
结果的能力,包括我正在寻找的'Write':
在本文中,它被标记为包含在仍未发布的16.0.P2 release milestone中。