【问题标题】:Error CS0433 The type 'Task' exists in both System.Threading and mscorlib错误 CS0433 System.Threading 和 mscorlib 中都存在“任务”类型
【发布时间】:2015-12-19 21:19:39
【问题描述】:

我在这段代码中遇到错误:

  public async Task SendEmailsTask(List<string> emails)
    {
        for (int i = 0; i < emails.Count; i++)
        {

                await Task.Delay(5000);
        }
    }

这是错误:

Severity    Code    Description Project File    Line    Source  Suppression State Error CS0433  The type 'Task' exists in both 'System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

【问题讨论】:

  • 你引用了哪些命名空间?
  • 使用 myAlias = System.Threading;
  • 我在 System.Threading 命名空间中根本找不到对 Task 的引用。应该是System.Threading.Tasks
  • 您不应该使用 System.Threading.dll,它旨在为面向 .NET 3.5 的应用程序添加任务支持。不清楚它来自哪里,可能是一个糟糕的 Nuget 包。尝试通过简单地将其从项目的引用中删除来取得成功。

标签: c# namespaces


【解决方案1】:

编译器不知道您指的是哪个任务,因为它存在于两个库中。要么使用完整的类名 (System.Threading.Task SendEmailsTask),要么给命名空间一个别名

using myAlias = System.Threading.Tasks;

并将其引用为

public async myAlias.Task SendEmailsTask

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多