In my asp.net application I am creating a custom user control (which is similar to GridView). It takes list of an object (like Car, Person etc..) and shows them as table.
Since list of objects can be different type when I try to cast from a custom list to list of objects it gives me an error like: "Unable to cast object of type 'System.Collections.Generic.List1[Main.People]' to type 'System.Collections.Generic.List
1[System.Object]'."
Is there any better way to handle this?
Code is like:
public List<object> Objects = new List<object>();
protected void Page_Load(object sender, EventArgs e)
{
Objects = (List<object>) HttpContext.Current.Items["Objects"];
}
<table>
<% int counter = 0; %>
<% foreach (object customObject in Objects)
{ %>
<% counter++; %>
<%PropertyInfo[] propertyInfos = customObject.GetType().GetProperties(); %>
<%--header--%>
<% if (counter == 1)
{
%>
<tr>
<%
foreach (var reportField in propertyInfos)
{
%>
<th>
<%=reportField.Name %>
</th>
<%
}
%>
</tr>
<%
} %>
<%--data rows--%>
<tr>
<%
foreach (var reportField in propertyInfos)
{
%>
<td>
<a href="?page=Details.aspx?<%=reportField.Name %>=<%=reportField.GetValue(customObject, null) %>">
<%=reportField.GetValue(customObject, null)%>
</a>
</td>
<%
}
%>
</tr>
<% }%>
</table>