这是我见过的最疯狂的事情,因为 Fody 插件通过发出无效代码破坏了我的程序集,并且控制流在运行时随机变化......这次没有 Fody。
事实:
- 整个故事都在一个项目中。
- GetMessage 扩展方法已存在数周...
- 问题从 2 小时开始出现,我无法弄清楚它是什么。
- 只有一种 GetMessage 扩展方法。
错误消息(见图片)列出了两个相同方法规范
错误 CS0121 以下方法或属性之间的调用不明确:“ComiCalc.Data.ExceptionExtensions.GetMessage2(System.Exception)”和“ComiCalc.Data.ExceptionExtensions.GetMessage2(System.Exception)”ComiCalc。数据D:\2014Develop\.vsonline\ComiCalc\src\ComiCalc.Data\Services\UserService.cs 61
如果我将调用和方法定义(仅在 2 个地方进行了 2 次编辑)更改为 GetMessage2,那么我会收到完全相同的错误消息,只是引用 GetMessage2。
- 使用 VS 2015
有什么想法吗?
这是唯一的方法:
namespace ComiCalc.Data
{
using System;
using System.Data.Entity.Validation;
using PluralTouch.DataAccess;
// TODO: Move to PluralTouch
public static class ExceptionExtensions
{
public static string GetMessage2(this Exception exception)
{
var message = exception.Message;
if (exception is DbEntityValidationException)
{
message = ((DbEntityValidationException) exception).DbEntityValidationResultToString();
}
return message;
}
}
}
最佳答案
确保您没有在项目引用中引用输出二进制文件(即项目引用本身)。这在我过去使用 Resharper 时发生过(将输出二进制文件添加到项目引用中),因此扩展方法既在源代码中又在二进制引用中。
关于C# 编译器 : CS0121: The call is ambiguous between the following methods or properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31729186/