Visual Studio C#编译器警告无意中将变量分配给自身,但此警告不适用于C#属性,仅适用于变量.如其他问题所述.
但是,如果我为自己分配一个属性,我真的会喜欢类似的东西,可以在编译时警告我.
我目前正在使用Visual Studio 2013,但如果解决方案至少在Visual Studio 2015中有效,我也没关系.另外,我不使用像ReSharper或CodeRush这样的第三方商业插件,所以我更喜欢不使用的解决方案涉及购买东西,但我愿意接受建议.
你知道我怎么能做到这一点?
我非常习惯使用只读公共"检查"属性来构建接收器依赖注入模式来存储接收到的依赖项.
例如,假设一个Foo依赖于ILogger实现的类.该logger实例在构造函数中提供给类,构造函数检查空值并将依赖项存储在名为的实例属性中Logger:
public class Foo
{
public ILogger Logger { get; private set; }
public Foo(ILogger logger)
{
if(logger == null) throw new ArgumentNullException("logger");
this.Logger = logger;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我经常输入错误,将属性分配给自己而不是传递给构造函数的参数.
public class Foo
{
public ILogger Logger { get; private set; }
public Foo(ILogger logger)
{
if(logger == null) throw new ArgumentNullException("logger");
this.Logger …Run Code Online (Sandbox Code Playgroud) c# ×1