在下面的示例中,Resharper 提示 DoA()
方法从未被使用过,尽管它是由“AImplementator”类实现的:
namespace Test
{
public interface A
{
// Method 'DoA' is never used
void DoA();
}
public class AImplementator: A
{
public void DoA()
{
throw new System.NotImplementedException();
}
}
}
我无法理解这种行为,因为正在使用该接口(interface)。
- 为什么会这样?
- 如何修复此警告?
Obs:我可以使用注释或 [UsedImplicitly]
属性来抑制警告。但鉴于这种情况,这两种选择似乎都不正确。我正在使用 Resharper 9.1。
最佳答案
此警告仅在 Solution-Wide Analysis is enabled 时显示.
警告是关于 interface 方法的用法(在本例中为调用)。下面的示例应该演示它(注意 var
与接口(interface)作为局部变量类型)。
var instance = new AImplementator();
// Does NOT make A.DoA() "used":
instance.DoA();
A instanceAsInterface = new AImplementator();
// DOES make A.DoA() "used":
instanceAsInterface.DoA();
关于c# - 如何避免 'Method is never used' 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30673904/