I have a list, the type of the object will be found only at the runtime through reflection. But when I try to assign the list to the actual entity, it is throwing error as "object cannot be converted". Below is the code,
var obj = new List<Object>();
obj.Add(cust1);
obj.Add(Cust2);
Type newType = t.GetProperty("Customer").PropertyType// I will get type from property
var data= Convert.ChangeType(obj,newType); //This line throws error`
Your
obj
object ist not aCustomer
, it's aList
ofCustomer
. So you should get the type of it this way:But you couldn't convert your object to to this
listType
, you will get anException
, thatList
doesn't implementIConvertible
interface.Solution is: just to create new list and copy all data to it: