相关疑难解决方法(0)

以下方法或属性之间的调用是不明确的(bug ??)

  1. 创建一个新的ASP.NET MVC Web应用程序
  2. 创建一个ASP.NET App_Code文件夹
  3. 在新文件夹中,使用扩展方法创建一个类.例如:

    static public class BugMVCExtension
    {
        public static int ToInt(this string str)
        {
            return Convert.ToInt32(str);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 选择一个视图并尝试使用这个新的扩展方法

你会得到这个例外:

CS0121: The call is ambiguous between the following methods or properties:
'*MvcApplication1.App_code.BugMVCExtentions.ToInt(string)*' and
'*MvcApplication1.App_code.BugMVCExtentions.ToInt(string)*'
Run Code Online (Sandbox Code Playgroud)

这里有人有关于它的更多信息吗?在ASP.NET MVC(?)Web应用程序中创建App_code是错误的吗?

.net c# asp.net-mvc .net-3.5

33
推荐指数
1
解决办法
4万
查看次数

当只有一个具有给定名称的方法时,Visual Studio 声明存在不明确的调用

我试图弄清楚为什么会发生这种情况,但我不能。我有一个 WCF 服务,在其中我创建了一个名为 ExtensionUtil 的类,它仅包含扩展方法。

现在,我在 ExntensionUtil 类中拥有的唯一方法称为 AddWithNullableValue。它看起来像这样:

public static void AddWithNullableValue(this SqlParameterCollection paramCollection, string paramName, object value)
{
    SqlParameter param = new SqlParameter();

    param.ParameterName = paramName;
    param.Value = value == null ? DBNull.Value : value;

    paramCollection.Add(param);
}
Run Code Online (Sandbox Code Playgroud)

我在 DAL 中使用这种方法是这样的:

sqlCommand.Parameters.AddWithNullableValue("@categoryId", publication.CategoryId);
Run Code Online (Sandbox Code Playgroud)

一切都编译得很好。但是,当我尝试启动该服务时出现编译错误,说明:

The call is ambiguous between the following methods or properties:
 'App_Code.Util.ExtensionUtil.AddWithNullableValue(System.Data.SqlClient.SqlParameterCollection, string, object)' and
 'App_Code.Util.ExtensionUtil.AddWithNullableValue(System.Data.SqlClient.SqlParameterCollection, string, object)'
Run Code Online (Sandbox Code Playgroud)

为什么会这样?我只有一个具有此名称的方法,编译器说该调用不明确。

编辑:

当我删除 SqlParameterCollection 参数前面的“this”关键字时,使该方法成为普通的静态方法,而不是扩展方法,一切正常。此外,我查看了整个解决方案(使用 Ctrl+Shift+F)以查找所有出现的“AddWithNullableValues”关键字,搜索结果如下:方法的签名和代码中我调用它的两个地方。然后我什至创建了一个全新的静态类,添加了具有完全不同名称和相同主体的相同方法,但在调用它时仍然出现相同的错误,因此问题一定是其他问题。

最后,我需要另一个扩展方法,当我在我的代码中调用它时,我得到了同样的错误。

public static Nullable<T> GetNullableValue<T>(this SqlDataReader sqlDataReader, string columnName) where T : …
Run Code Online (Sandbox Code Playgroud)

c# wcf compilation

2
推荐指数
1
解决办法
1211
查看次数

标签 统计

c# ×2

.net ×1

.net-3.5 ×1

asp.net-mvc ×1

compilation ×1

wcf ×1