Вход на сайт
C# - вернуть типизированную коллекцию
1698 просмотров
Перейти к просмотру всей ветки
в ответ alex445 05.04.24 11:41, Последний раз изменено 05.04.24 11:56 (alex445)
Да, пачка ифов лучше. Ну, вроде теперь почти идеально.
public static IEnumerable<T> GetData<T>() where T : Base
{
if (typeof(T) == typeof(Derived1)) return (IEnumerable<T>)Derived1Collection;
else if (typeof(T) == typeof(Derived2)) return (IEnumerable<T>)Derived2Collection;
else throw new Exception($"{typeof(T)} is not supported.");
}