I am having trouble converting a list object back to its original type without an explicit cast. My intention is to generalize the methods so that I can go through different types of databases, such as Man or Company. I have no problem getting the values โโthrough reflection, but I need to convert the actual list back to the original type List<typeT>
instead of List<object>
in the method to which I pass it. I know the following methods:
SomeList.ConvertAll(x => (typeT)x); SomeList.Cast<typeT>().ToList();
However, for these methods I will have to explicitly impose typeT. I tried the following:
formatted1.ConvertAll(x => Convert.ChangeType(x, typeT));
and
List<dynamic> newList = new List<dynamic>(); foreach (var p in SomeList) { newList.Add(Convert.ChangeType(p, typeT); }
There is no success so far. Maybe I should have another game plan to summarize these methods. right now I am passing lists as follows:
public string returnJson(List<object> entity, string someString) { }
source share