为什么 Visual Studio 在 .Net Core 上运行的 WebAPI 代码中报告 lambda 错误?
Why does Visual Studio report a lambda error in a working WebAPI code on .Net Core?
我将 WebAPI 从 FullDotnet (4.6) 迁移到 .Net Core 2.0,我在使用 Dapper 的数据层中遇到了这个问题。
我的代码:
public List<Doctor> GetList()
{
List<Doctor> ret;
using (var db = GetMySqlConnection())
{
const string sql = @"SELECT D.Id, D.Bio, D.CRMState, D.CRMNumber,
U.Id, U.Email, U.CreatedOn, U.LastLogon, U.Login, U.Name, U.Phone, U.Surname, U.Id, U.Birth, U.CPF,
S.Id, S.Name
from Doctor D
inner join User U on U.Id = D.UserId
inner join Speciality S on S.Id = D.IDEspeciality
order by D.Id DESC";
ret = db.Query<Doctor, User, Speciality, Doctor>(sql, (doctor, user, speciality) =>
{
doctor.User = user;
doctor.Speciality = speciality;
return doctor;
}
, splitOn: "Id, Id", commandType: CommandType.Text).ToList();
}
return ret;
}
奇怪的行为:
解决方案构建和工作
VisualStudio 突出显示的 "error" 是:
Argument type 'lambda expression' is not assignable to parameter type
'System.Func`5'
我有另一个 类 具有相同的行为和相同的错误,但是,同样,代码 编译和工作, 但是这个 "error highlight" 很烦人!
我的旧解决方案中没有这个亮点 运行 .NET Framework 4.6
有用信息:
- Class 库:.Net Standard 2.0
- Visual Studio 2017 v15.4.1
- Resharper 2017.2
正如我们在评论中发现的那样,问题是由 ReSharper (2017.2) 引起的。
当怀疑 ReSharper 有问题时,您可以采取一些额外的步骤。
Suspend/resume ReSharper
为了确认是 ReSharper 引起了问题,您可以使用以下方法暂停它:
Tools/Options.../Resharper node/Suspend now
在这种情况下,执行 suspend/resume 循环就足够了,问题就解决了。
正在清除 ReSharper 缓存 (source)
Broken caches affect ReSharper behavior. For example, ReSharper can
stop resolving symbols or some navigation commands can be unavailable.
If you notice such strange behavior, clearing caches for the current
solution could help in most cases.
To clear caches for the current solution
- Open the solution with supposedly broken caches in Visual Studio.
- Open the Environment | General page of ReSharper options.
- Click Clear caches. Note that the caches will be only cleaned in the currently selected cache location.
- Reopen your solution for the changes to take effect.
ReSharper also cleans up solution caches automatically if a
specific solution was not open for more than 30 days.
上面的方法不行怎么办?
如果您确认这是一个 ReSharper 问题,那么您应该首先尝试更新到 ReSharper 的最新版本并尝试重现您的问题。
如果问题仍然存在,那么您应该前往 JetBrains bug tracker 并在确认它不是已报告的错误后提交问题。
我将 WebAPI 从 FullDotnet (4.6) 迁移到 .Net Core 2.0,我在使用 Dapper 的数据层中遇到了这个问题。
我的代码:
public List<Doctor> GetList()
{
List<Doctor> ret;
using (var db = GetMySqlConnection())
{
const string sql = @"SELECT D.Id, D.Bio, D.CRMState, D.CRMNumber,
U.Id, U.Email, U.CreatedOn, U.LastLogon, U.Login, U.Name, U.Phone, U.Surname, U.Id, U.Birth, U.CPF,
S.Id, S.Name
from Doctor D
inner join User U on U.Id = D.UserId
inner join Speciality S on S.Id = D.IDEspeciality
order by D.Id DESC";
ret = db.Query<Doctor, User, Speciality, Doctor>(sql, (doctor, user, speciality) =>
{
doctor.User = user;
doctor.Speciality = speciality;
return doctor;
}
, splitOn: "Id, Id", commandType: CommandType.Text).ToList();
}
return ret;
}
奇怪的行为:
解决方案构建和工作 VisualStudio 突出显示的 "error" 是:
Argument type 'lambda expression' is not assignable to parameter type 'System.Func`5'
我有另一个 类 具有相同的行为和相同的错误,但是,同样,代码 编译和工作, 但是这个 "error highlight" 很烦人!
我的旧解决方案中没有这个亮点 运行 .NET Framework 4.6
有用信息:
- Class 库:.Net Standard 2.0
- Visual Studio 2017 v15.4.1
- Resharper 2017.2
正如我们在评论中发现的那样,问题是由 ReSharper (2017.2) 引起的。
当怀疑 ReSharper 有问题时,您可以采取一些额外的步骤。
Suspend/resume ReSharper
为了确认是 ReSharper 引起了问题,您可以使用以下方法暂停它:
Tools/Options.../Resharper node/Suspend now
在这种情况下,执行 suspend/resume 循环就足够了,问题就解决了。
正在清除 ReSharper 缓存 (source)
Broken caches affect ReSharper behavior. For example, ReSharper can stop resolving symbols or some navigation commands can be unavailable. If you notice such strange behavior, clearing caches for the current solution could help in most cases.
To clear caches for the current solution
- Open the solution with supposedly broken caches in Visual Studio.
- Open the Environment | General page of ReSharper options.
- Click Clear caches. Note that the caches will be only cleaned in the currently selected cache location.
- Reopen your solution for the changes to take effect.
ReSharper also cleans up solution caches automatically if a specific solution was not open for more than 30 days.
上面的方法不行怎么办?
如果您确认这是一个 ReSharper 问题,那么您应该首先尝试更新到 ReSharper 的最新版本并尝试重现您的问题。
如果问题仍然存在,那么您应该前往 JetBrains bug tracker 并在确认它不是已报告的错误后提交问题。