IQueryable OfType where T is a runtime Type

后端 未结 7 625
旧巷少年郎
旧巷少年郎 2020-12-14 01:52

I need to be able to get something similar to the following to work:

Type type = ??? // something decided at runtime with .GetType or typeof;
object[] entity         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 02:41

    Looks like you’ll need to use Reflection here...

    public static IEnumerable DyamicOfType(
            this IQueryable input, Type type)
    {
        var ofType = typeof(Queryable).GetMethod("OfType",
                         BindingFlags.Static | BindingFlags.Public);
        var ofTypeT = ofType.MakeGenericMethod(type);
        return (IEnumerable) ofTypeT.Invoke(null, new object[] { input });
    }
    
    Type type = // ...;
    var entityList = context.Resources.DynamicOfType(type).ToList();
    
        

    提交回复
    热议问题