我目前正在编写使用 dynamic-linq 的代码,我在使用 List<BaseClass>
时遇到了问题,其中列表实际上包含 Person
的列表类。
当我执行以下代码时,我得到 ParseException
:
var list = new List<BaseClass>();
list.Add(new Person
{
FirstName = "Joe",
Surname = "Bloggs"
});
list.Where("FirstName == @0", "Joe");
还有异常(exception):
请参阅BaseClass
如下:
public class BaseClass
{
public int Id { get; set; }
}
还有Person
类:
public class Person : BaseClass
{
public string FirstName { get; set; }
public string Surname { get; set; }
}
我可以通过实现以下代码来克服该错误:
var list = new List<BaseClass>();
list.Add(new Person
{
FirstName = "Joe",
Surname = "Bloggs"
});
var newList = CreateListOfCorrectType<BaseClass>(list);
newList.Where("FirstName == @0", "Joe");
请参阅CreateListOfCorrectType<T>
方法如下:
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;
}
我的问题是是否使用CreateListOfCorrectType
是解决问题的最好方法吗?如果不是,我有什么替代方案来获得 List<BaseClass>
到正确的类型。
我希望将其与现有代码一起使用,并更改现有的 List<>
类型是不可能的。还有CreateListOfCorrectType
方法不知道Person
类。
请注意,类名称和变量仅用于演示目的。
更新
下面乐观主义者的回答引导我找到了问题的解决方案,请参阅下面使用的扩展方法:
public static IList ToDerivedListType(this IList list)
{
if (list == null || list.Count == 0)
{
return list;
}
var type = list.Cast<object>().FirstOrDefault().GetType();
var castedList = typeof(Enumerable).GetMethod("Cast", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
.MakeGenericMethod(type)
.Invoke(null, new[] { list });
return typeof(Enumerable).GetMethod("ToList", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
.MakeGenericMethod(type)
.Invoke(null, new[] { castedList }) as IList;
}
System.Linq.Enumerable.Cast
和MakeGenericMethod
是关键。
最佳答案
如何使用 OfType linq 方法:
list.OfType<Person>().Where("FirstName == @0", "Joe");
参见https://msdn.microsoft.com/en-us/library/vstudio/bb360913(v=vs.100).aspx
关于c# - 将 List 转换为 typeof UnderlyingSystemType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30893774/