从重新选择的类型实例化一个IQueryable <>

时间:2018-07-05 09:23:01

标签: c#

我有一个ClassName字符串。我创建了一个反射的List对象:

Type objectType = Type.GetType(ClassName);
var listObjectType = typeof(List<>);
var constructedListObjectType = listObjectType.MakeGenericType(objectType);
object listObject = Activator.CreateInstance(constructedListObjectType);

现在我希望listObject成为IQueryable<ClassName>而不是List<ClassName>

3 个答案:

答案 0 :(得分:2)

不清楚要做什么,但是您要求了一些内容,您就会得到要求的内容。

string className = "System.String";
Type type = Type.GetType(className);
var listType = typeof(List<>).MakeGenericType(type);

// This is a List<type> but it is easier to handle it through
// its non-generic IEnumerable interface
var list = (IEnumerable)Activator.CreateInstance(listType);

// Note that in truth this will return a IQueryable<type>
IQueryable queryable = Queryable.AsQueryable(list);

Queryable.AsQueryable有两个重载。一个要求IEnumerable<T>并返回相应的IQueryable<>,另一个要求IEnumerable,检查实际上是否为IEnumerable<T>并返回向下转换为的IQueryable<T> IQueryable。我正在使用第二个。

答案 1 :(得分:1)

此帮助程序方法应执行您想要的操作。在CreateList内,您可以使用强类型列表执行您喜欢的操作。 class Chat extends JButton { private static final long serialVersionUID = 1L; public Group group; public Chat(Group group) { this.group = group; setMinimumSize(new Dimension(300, 50)); setFocusPainted(false); setText(group.groupname); setFont(new Font("Tahoma", Font.PLAIN, 30)); setForeground(Manager.dark); setBackground(blue); setBorder(new MatteBorder(0, 0, 1, 0, Manager.ct.getForeground())); } } public void initGroups() { int count = 0; for(Group group : groups) { if(group != null) { //TODO: ADD CHATS (GROUPS) TO SCROLLPANE count ++; } } if(count == 0) { System.out.println("No groups"); } } 将返回可查询的强制转换作为对象。

显然,请确保您使用了正确的用法,以便GetQuerable可以正常工作

AsQuerable()

要使用:

public static class Helper
{

    public static object GetQueryable(string type) {
        var method = typeof(Helper).GetMethod(nameof(CreateList));
        return method.MakeGenericMethod(Type.GetType(type)).Invoke(null, new object[0]);
    }

    public static IQueryable<T> CreateList<T>()
    {
        return new List<T>().AsQueryable();
    }
}

答案 2 :(得分:0)

这对您有用吗?

    [Fact]
    public void Test()
    {
        var listGenericParameter = typeof(ClassName);
        var listObjectType = typeof(List<>);
        var constructedListObjectType = listObjectType.MakeGenericType(listGenericParameter);
        var listInstance = Activator.CreateInstance(constructedListObjectType);
        var asQueryableMethod = typeof(Queryable).GetMethod(nameof(Queryable.AsQueryable), new[] { typeof(IEnumerable) }) ??
                                throw new InvalidOperationException($"Could not find method AsQueryable() on type {nameof(Queryable)}");

        var listAsQueryable = asQueryableMethod.Invoke(null, new [] { listInstance });
        // method.Invoke('method owner instance', 'method args') 
        // first arg is null because AsQueryable() is extension method = static 
        // => there is no instance "owning" the method (just an owner type)

        (listAsQueryable is IQueryable<ClassName>).Should().BeTrue();
    }

    public class ClassName { }

编辑:修复了在xonos发表评论后代码不会误导的问题