Mam dwie metody rozszerzenia:metody rozszerzeń przeciążać wybór
public static IPropertyAssertions<T> ShouldHave<T>(this T subject)
{
return new PropertyAssertions<T>(subject);
}
public static IPropertyAssertions<T> ShouldHave<T>(this IEnumerable<T> subject)
{
return new CollectionPropertyAssertions<T>(subject);
}
Teraz mam napisać kod, który wykorzystuje go:
List<Customer> collection2 = new List<Customer>();
collection2.ShouldHave(); //first overload is chosen
IEnumerable<Customer> collection3 = new List<Customer>();
collection3.ShouldHave(); //second overload is chosen
drugie przeciążenie jest wybrany tylko jeśli jawnie określić typ IEnumerable. Czy istnieje sposób, aby w obu przypadkach wybrać drugie przeciążenie?
Nie będzie różnicy między 'collection1' i' collection2'. Są dokładnie tym samym kodem, tylko napisane inaczej. – svick
@svick, tak, ale chciałem pokazać wszystkie opcje składni – SiberianGuy
Należy pamiętać, że fakt, że jest to metoda rozszerzenia, jest nieistotny; rozdzielczość przeciążenia obsługuje ją jak normalną metodę statyczną. –