Entity framework 如何在Ilist中通过T dynamicali<;T>;?

Entity framework 如何在Ilist中通过T dynamicali<;T>;?,entity-framework,linq-to-sql,entity-framework-4.1,Entity Framework,Linq To Sql,Entity Framework 4.1,我有一个问题。我有一个方法(过滤器),我想通过T动态。但它不接受。我怎么做 public List<T> Filter<T>(string TypeOfCompare) { List<T> ReturnList2 = new List<T>(); return ReturnList2; } IList MakeListOfType(Type listType) { Ty

我有一个问题。我有一个方法(过滤器),我想通过T动态。但它不接受。我怎么做

 public List<T> Filter<T>(string TypeOfCompare)
    {


        List<T> ReturnList2 = new List<T>();
         return ReturnList2;
    }
IList MakeListOfType(Type listType)
    {
        Type listType1 = typeof(List<>);
        Type specificListType = listType.MakeGenericType(listType1);

        return (IList)Activator.CreateInstance(specificListType);
    }
 Filter < ConstructGenericList(h) > ("s");
公共列表筛选器(比较的字符串类型)
{
List ReturnList2=新列表();
返回列表2;
}
IList MakeListOfType(类型listType)
{
类型listType1=类型(列表);
类型specificListType=listType.MakeGenericType(listType1);
return(IList)Activator.CreateInstance(specificListType);
}
过滤器(“s”);

泛型参数必须具有可在编译时确定的类型(而不必像某些其他语言那样诉诸函数类型推断)。所以,你不能仅仅在尖括号之间粘贴一个函数来获得你想要的类型

编辑:

既然我知道你想做什么,我会建议一个完全不同的方法

您提到您正在使用实体框架,并且您正在尝试使用一种方法来获取不同类型对象的列表。这些对象——比如学生和教师——必须有一些共同点,否则您就不会尝试使用相同的方法来检索它们的列表。例如,您可能只是想显示一个名称,并有一个ID用作键

在这种情况下,我建议定义一个接口,该接口具有学生、教师等实际需要的公共属性,然后返回该接口类型的列表。在该方法中,基本上使用的是factory模式的变体

因此,您可以定义如下接口:

public interface INamedPerson
{
    int ID { get; }
    string FirstName { get; }
    string LastName { get; }
}
使您的实体实现此接口。自动生成的实体(通常)是分部类,因此在您自己的新代码文件(而不是自动生成的代码文件本身)中,您可以执行以下操作:

public partial class Student : INamedPerson
{
    public int ID
    {
        get
        {
            return StudentId;
        }
    }
}

现在,如果您已经有了
ID
属性,您甚至不需要添加它。但是,如果每个类中的identity属性不同,则此适配器可以是实现所需接口的一种方法

然后,对于方法本身,示例如下:

public List<INamedPerson> MakeListOfType(Type type)
{
    if (type == typeof(Student))
    {
        // Get your list of students.  I'll just use a made-up
        // method that returns List<Student>.
        return GetStudentList().Select<Student, INamedPerson>(s => (INamedPerson)s)
                               .ToList<INamedPerson>();
    }
    if (type == typeof(Teacher))
    {
        return GetTeacherList().Select<Teacher, INamedPerson>(t => (INamedPerson)t)
                               .ToList<INamedPerson>();
    }
    throw new ArgumentException("Invalid type.");
}
当然,使用此函数再次要求在编译时知道传入的类型。然而,在某个时候,你必须决定一种类型,所以这可能是合适的时机。此外,您可以在方法调用时取消泛型类型,从而简化您的工作;只要您传入一个不带参数的函数,并返回实现INamedPerson的同一类型对象的
IEnumerable
,编译器就可以确定泛型类型
T
使用什么

IList MakeListOfType(Type listType)
{
    Type listType1 = typeof(List<>);
    Type specificListType = listType.MakeGenericType(listType1);

    return (IList)Activator.CreateInstance(specificListType);
}

(请注意,我更改了变量名称以使代码更清晰)

您不能;泛型是在编译时检查的,这就是关键。这并不是说没有更好的方法来完成你所需要的。与其给我们一个毫无意义的建议解决方案,不如告诉我们您试图在高层次上实现什么。我有一个复选框,项目是学生、教师……他们是我的实体。我想当用户选择其中一个时,我得到(例如用户选择学生)名称,并通过MakeListOfType方法列出学生,我想将列表传递给我的筛选方法并执行一些操作。我如何才能做到这一点?
public List<INamedPerson> GetListFromFunction<T>(Func<IEnumerable<T>> theFunction) where T : INamedPerson
{
    return theFunction().Select<T, INamedPerson>(t => (INamedPerson)t).ToList<INamedPerson>();
}
IList MakeListOfType(Type listType)
{
    Type listType1 = typeof(List<>);
    Type specificListType = listType.MakeGenericType(listType1);

    return (IList)Activator.CreateInstance(specificListType);
}
IList MakeListOfType(Type elementType)
{
    Type listType = typeof(List<>);
    Type specificListType = listType.MakeGenericType(elementType);

    return (IList)Activator.CreateInstance(specificListType);
}