Login
C# - вернуть типизированную коллекцию
1384 просмотров
Перейти к просмотру всей ветки
alex445 патриот
in Antwort alex445 05.04.24 11:41, Zuletzt geändert 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."); }