Teoman shipahi
Teoman shipahi

Reputation: 23132

casting list<custom object> to list<object>

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.List1[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>

Upvotes: 2

Views: 1922

Answers (3)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10862

Make all your classes: Cars, People inherit from a base class MyBaseClass and cast your List to that type/class

public List<MyBaseClass> Objects = new List<MyBaseClass>();

protected void Page_Load(object sender, EventArgs e)
{
    Objects = (List<MyBaseClass>) HttpContext.Current.Items["Objects"];
}

Then you can iterate your List and determine the exact type

foreach(MyBaseClass mbc in Objects)
{
    if(mbc is People)
    {
        //its a People object
    }
    else if(mbc is Car)
    {
        //ist a Car object
    }
}

Keep in mind that in your code when you are adding or creating the Objects list you need to declare it as

List<MyBaseClass> Objects 

By the way it seems that you current is list is a list of People so this should work

public List<People> Objects = new List<People>();

protected void Page_Load(object sender, EventArgs e)
{
    Objects = (List<People>) HttpContext.Current.Items["Objects"];
}

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149108

Since it appears you're not actually modifying the list in your code, you don't actually need it to be a list. It can just be an IEnumerable<object>:

public IEnumerable<object> Objects;
protected void Page_Load(object sender, EventArgs e)
{
    Objects = (IEnumerable<object>)HttpContext.Current.Items["Objects"];
}

Upvotes: 1

Eric J.
Eric J.

Reputation: 150238

You should be able to use the Cast() extension method in Linq

Objects = HttpContext.Current.Items["Objects"].Cast<object>().ToList()

http://msdn.microsoft.com/en-us/library/bb341406.aspx

Upvotes: 2

Related Questions