I am trying to dynamically show a table depending on what tableName user has selected from dropdown. I am passing a json object from my web Controller(.Net Core) so in order to do it, I am first converting my dataTable to list of objects using function
- public static List<T> ConvertTableToList<T>(this DataTable table) where T : class, new()
- {
- try { List<T> list = new List<T>();
- foreach (var row in table.AsEnumerable())
- {
- T obj = new T();
- foreach (var prop in obj.GetType().GetProperties())
- {
- try { PropertyInfo propertyInfo = obj.GetType().GetProperty(prop.Name);
- propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- list.Add(obj);
- }
- return list;
- }
- catch { return null; }
- }
and call this function in my Get Request
- public IActionResult GetTableDetailsByTableName(string TableNameSelected)
- {
-
- TableDetails = ReferenceTableLogics.getTableDetails(TableNameSelected);
- var TableDetailsInList = ConverterClass.ConvertTableToList<CSBM_AGE_BAND>(TableDetails);
- return Ok(TableDetailsInList);
- }
Now the issue is that I need to tell my class Name (eg CSBM_AGE_BAND in this case) depending on what user has selected in dropdown (TableNameSelected).
Is there any way by which I can dynamically pass this class name to my function ConvertTableToList() ?