简体   繁体   English

构造函数DI - Field永远不会分配给,并且始终具有其默认值

[英]Constructor DI - Field is never assigned to, and will always have its default value

I have the following code: 我有以下代码:

private IMemoryCache _cache;

public HomeController(IMemoryCache memoryCache) : base() {    
   this._cache = memoryCache;    
}

ASP.Net Core is handling the injection of the IMemoryCache instance via: ASP.Net Core方式处理IMemoryCache实例的注入:

public void ConfigureServices(IServiceCollection services) {
   services.AddMemoryCache();
}

It is flagging the _cache field with: 它正在标记_cache字段:

Field HomeController._cache is never assigned to, and will always have its default value null 字段HomeController._cache永远不会分配给,并且将始终具有其默认值null

It's not causing any errors, it is just distracting in the editor. 它没有造成任何错误,只是在编辑器中分散注意力。

What is the way to go about convincing Visual Studio 2017 that I am in fact assigning this field a value, via the constructor? 有什么方法可以说服Visual Studio 2017我实际上是通过构造函数为这个字段赋值?

Code (because the error overlaps it): 代码(因为错误重叠):

在此输入图像描述

With the editor showing the error: 编辑器显示错误:

在此输入图像描述

At runtime showing the valid instance being injected: 在运行时显示正在注入的有效实例:

在此输入图像描述

Edit: 编辑:

For the comments suggesting to use: 对于建议使用的评论:

private IMemoryCache _cache = null;

... this makes no difference, same exact result. ......这没什么区别,同样的确切结果。

Restarting Visual Studio 2017 and rebooting did not resolve the issue. 重新启动Visual Studio 2017并重新启动无法解决问题。

However, I was notified of Visual Studio 2017 update version 15.3.3 when the environment reloaded, which I installed. 但是,当我重新加载环境时,我收到了Visual Studio 2017更新版本15.3.3的通知。

After re-starting Visual Studio post-update, this situation is no longer occurring. 重新启动Visual Studio后更新,这种情况不再发生。

I doubt the update actually did anything related to this problem, it's more likely something in my VS configuration was off and the update put things back in place. 我怀疑更新实际上做了与此问题相关的任何事情,更可能是我的VS配置中的某些内容已关闭且更新将事情重新放回原位。

Cleaning your solution should solve this. 清理解决方案应解决此问题。 In VS2017 menus: Project -> Clean Solution . 在VS2017菜单中: Project -> Clean Solution

I had the same case on VS2015. 我在VS2015上遇到了同样的情况。 In my case Build -> Rebuild Solution resolved the issue. 在我的案例中, Build -> Rebuild Solution解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构造函数混淆 - '从未分配给,并且将始终具有其默认值' - Constructor Confusion - 'never assigned to, and will always have its default value' 字段“ FIELD”从未分配,并且将始终具有其默认值 - field 'FIELD' is never assigned to, and will always have its default value 字段从未分配给它,并且将始终具有其默认值 - Field is never assigned to, and will always have its default value 警告:永远不会将字段分配给,并且始终将其默认值设置为null - Warning: Field is never assigned to, and will always have its default value null 字段'xxx'永远不会分配给,并且将始终具有其默认值null - Field 'xxx' is never assigned to, and will always have its default value null 永远不会将字段xxx分配给,并始终将其默认值设置为null - Field xxx is never assigned to, and will always have its default value null 字段…永远不会分配给它,并且其默认值始终为null - Field … is never assigned to, and will always have its default value null 该字段永远不会分配给它,并且其默认值始终为null - The Field Is Never Assigned To And Will Always Have Its Default Value null 字段从未分配给它,并且将始终具有其默认值null - Field is never assigned to, and will always have its default value null 字段modifyDate永远不会分配给,并且始终具有其默认值0 - Field modifyDate is never assigned to, and will always have its default value 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM