Sử dụng .NET 4, tôi bị nhầm lẫn do không có trình biên dịch để giải quyết lời gọi phương thức đầu tiên trong mẫu bên dưới.Vấn đề về độ phân giải phương pháp với thông số mặc định và generics
using System;
namespace MethodResolutionTest
{
class Program
{
static void Main(string[] args)
{
NonGeneric foo = null;
// ambiguous
foo.Ext1(x => new NonGeneric());
// resolves to first Ext1
foo.Ext1(x => new NonGeneric(), 1);
// resolves to first Ext2
foo.Ext2(x => new NonGeneric());
// resolves to first Ext2
foo.Ext2(x => new NonGeneric(), 1);
// resolves to second Ext2
foo.Ext2(x => "foo");
// resolves to second Ext2
foo.Ext2(x => "foo", 1);
// resolves to first Ext3
foo.Ext3(x => new NonGeneric());
// resolves to first Ext3
foo.Ext3(x => new NonGeneric(), 1);
// resolves to second Ext3
foo.Ext3(x => "foo");
// resolves to second Ext3
foo.Ext3(x => "foo", 1);
}
}
public class NonGeneric
{
}
public class Generic<T> : NonGeneric
{
}
public static class Extensions1
{
public static NonGeneric Ext1(this NonGeneric first, Func<NonGeneric, NonGeneric> getNext, int i = 0)
{
return null;
}
public static Generic<TNext> Ext1<TNext>(this NonGeneric first, Func<NonGeneric, TNext> getNext, int i = 0, string s = null)
{
return null;
}
}
// only difference between Extensions2 and Extensions1 is that the second overload no longer has a default string parameter
public static class Extensions2
{
public static NonGeneric Ext2(this NonGeneric first, Func<NonGeneric, NonGeneric> getNext, int i = 0)
{
return null;
}
public static Generic<TNext> Ext2<TNext>(this NonGeneric first, Func<NonGeneric, TNext> getNext, int i = 0)
{
return null;
}
}
// Extensions3 explicitly defines an overload that does not default the int parameter
public static class Extensions3
{
public static NonGeneric Ext3(this NonGeneric first, Func<NonGeneric, NonGeneric> getNext)
{
return Ext3(first, getNext, default(int));
}
public static NonGeneric Ext3(this NonGeneric first, Func<NonGeneric, NonGeneric> getNext, int i = 0)
{
return null;
}
public static Generic<TNext> Ext3<TNext>(this NonGeneric first, Func<NonGeneric, TNext> getNext, int i = 0)
{
return null;
}
}
}
Có ai có thể làm sáng tỏ điều này không? Tôi nghi ngờ tôi không thực sự có một con đường phía trước ở đây khác hơn là sửa đổi các API của tôi để giúp trình biên dịch (theo Extensions3
ở trên), nhưng nếu có một cách dễ dàng hơn/tốt hơn sau đó tôi rất muốn nghe nó.
Trình biên dịch chọn tốt trong cả 'Extensions2' và' kịch bản Extensions3', vì vậy nó không thật đơn giản. Ngoài ra, nếu tôi không muốn tham số 'int' mặc định, thì rõ ràng tôi sẽ không tuyên bố nó theo cách đó ngay từ đầu! –
Nhưng tại sao bạn có hai phương pháp với các tham số tùy chọn có hiệu quả là mơ hồ nếu các tham số tùy chọn bị bỏ qua? Nếu bạn hoàn toàn cần ** cả hai phương thức, bạn sẽ cần sử dụng các giải pháp 'Extensions2' hoặc' Extensions3'. – khellang
@ khellang: bạn có thể chỉ ra các phần đặc tả ngôn ngữ C# dẫn đến hành vi như vậy (sự mơ hồ về độ phân giải quá tải) không? –