如何解决通用扩展方法中的这种歧义?

时间:2014-01-16 09:04:02

标签: c# generics extension-methods

private static void TestAmbiguousGenerics()
{
    var string1 = "Hello".Foo(s => s + " world"); // works
    int? n = 123;
    var string2 = n.Foo(x => (x + 1).ToString()); // compiler error
    Console.WriteLine("{0} {1}".Fmt(string1, string2));
}

public static string Foo<T>(this T obj, Func<T, string> func) where T : class
{
    return obj == null ? "" : func(obj);
}
public static string Foo<T>(this T? str, Func<T, string> func) where T : struct
{
    return str == null ? "" : func(str.Value);
}

编译器无法判断我是Foo<T> T Nullable<int>T的第一次重载,还是int n.Foo<int>()的第二次重载}}。显然,我可以通过明确调用Nullable<>来完成这项工作,但是有没有办法让第一次重载排除T而不限制{{1}}?

1 个答案:

答案 0 :(得分:6)

否 - 这两种方法都适用于过载分辨率;仅在选择最佳重载后检查类型参数约束。

有一个really evil way of working round this,但我个人要么给两个方法不同的名称​​或明确提供类型参数。