mercredi 17 juin 2015

Convert List to typeof UnderlyingSystemType

I am currently working on code that is using dynamic-linq, I ran into a problem when using a List<BaseClass>, where the list actually contains a list of the Person Class.

When I execute the following code I get a ParseException:

var list = new List<BaseClass>();

list.Add(new Person
{
  FirstName = "Joe",
  Surname   = "Bloggs"
});

list.Where("FirstName == @0", "Joe");

And the Exception:

enter image description here

Please see BaseClass below:

public class BaseClass 
{
    public int Id { get; set; }
}

And the Person class:

public class Person : BaseClass
{
   public string FirstName { get; set; }
   public string Surname   { get; set; }
}

I can overcome the error by implementing the following code:

var list = new List<BaseClass>();

list.Add(new Person
{
  FirstName = "Joe",
  Surname   = "Bloggs"
});            

var newList = CreateListOfCorrectType<BaseClass>(list);
newList.Where("FirstName == @0", "Joe");

Please see CreateListOfCorrectType<T> method below:

private IList CreateListOfCorrectType<T>(
         List<T> list) 
{
  if (list.Count == 0)
  {
    return list;
  }

  var typeInfo = list.FirstOrDefault().GetType();

  var correctListType   = typeof(List<>).MakeGenericType(typeInfo.UnderlyingSystemType);
  var listOfCorrectType = (Activator.CreateInstance(correctListType)) as IList;

  list.ForEach(x => listOfCorrectType.Add(x));

  return listOfCorrectType;
}

My Question is if using the CreateListOfCorrectType is the best way of overcoming the issue? and if not what alternatives do I have in getting the List<BaseClass> to the correct Type.

I am looking to use this with existing code, and changing the existing List<>types are not possible.

Please note that class names and variables are for demonstrative purposes only.





Aucun commentaire:

Enregistrer un commentaire