It will be a bit too much code, but do not worry =)
In, let's say, the application's data model is ("tables") a collection of instances of classes corresponding to certain database tables; they are updated from the database by Dapper, with a hard indication of the class in which instances the mapping will be used to place the results of the query, respectively, for each "table" there is a typical function that differs only in the name of the base entity. Naturally, this is not beautiful, and I want to summarize
public static OracleConnection oracleConnect;//общедоступный коннект к БД Оракл public ObservableCollection<LOGS> Logs;//"таблица" записей лога public IEnumerable<LOGS> getLogsRows(string sqlWhere = @"")//функция её обновления { string oracleSelectAllLogs = "SELECT * FROM LOGS " + sqlWhere; var getLogs = MNLZContext.oracleConnect.Query<LOGS>(oracleSelectAllLogs);//выбрать всё содержимое табл. return (getLogs); } public ObservableCollection<TRACK_EVENT> TrackEvent;//"таблица" событий public IEnumerable<TRACK_EVENT> getTrackEventRows(string sqlWhere = @"")//функция её обновления { string oracleSelectAllTrackEvent = "SELECT * FROM TRACK_EVENT " + sqlWhere; var trackEvent = MNLZContext.oracleConnect.Query<TRACK_EVENT>(oracleSelectAllTrackEvent);//выбрать всё содержимое табл. return (trackEvent); } //неудачная пока попытка "обобщения" public Dictionary<string, object> dbTables;//набор всех "таблиц", с доступом по имени табл. в БД public IEnumerable<baseTable> getTableRows(string tableName, string sqlWhere=@"") {//!собственно ПРОБЛЕМА: тут надо перейти от "baseTable" к чему-то вроде Type tableType = ((ObservableCollection<object>) dbTables[tableName]).GetType();//получим класс сущности элемента перечисления (тип таблицы), надеюсь //и далее вместо "baseTable" писать вроде "tableType.GetType()" -- но оно не работает, т.к. перечисления IEnumerable<T> недопустимы в данном контексте =( string oracleSelectAllTableRows = @"SELECT * FROM " + tableName + ' ' + sqlWhere; IEnumerable<baseTable> tableRows = MNLZContext.oracleConnect.Query<baseTable>(oracleSelectAllTableRows);//тут будет ошибка "перечисления IEnumerable<T> недопустимы в данном контексте", если пытаться заменить baseTable на tableType return (tableRows); } JFYI:
I suggest using it this way:
dbTables.Add("LOGS", Logs);//"таблицы" в словарь, с доступом по ключу == имени табл. в БД dbTables.Add("TRACK_EVENT", TrackEvent); foreach(string tableName in dbTables.Keys)//разом все "таблицы" обновить из БД {//так работать не будет, это просто примерный набросок dbTables[tableName] = new ObservableCollection<baseTable>( getTableRows(tableName) ); }; DB entity classes:
public partial class baseTable : Object //просто базовый класс для всех таблиц БД (пока только: LOGS, TRACK_EVENT) { public int ID { get; set; }//Первичный ключ } public partial class LOGS : baseTable //запись лога {//...с какими-то полями, это не существенно } public partial class TRACK_EVENT : baseTable //отслеживаемое событие {//...с какими-то полями, это не существенно } It seems to be a simple question, but something I don’t think - tell me how to wriggle out?