TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
somya rawal
NA
4
497
Pass Generic Object Dynamically
Apr 20 2018 5:17 PM
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)
{
//To get the data for the Table Selected DataTable
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() ?
Reply
Answers (
0
)
No session bound to the current context
Error while implementing shopping cart