简体   繁体   中英

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.List 1[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>

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

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"];
}

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"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM