0

I have a User class that has a GetQueryable method. Another method, Select(), calls GetQueryable(). I want to use the Select method without passing the type User to the Select method, because I have it in this but I can't use it.

Type type = this.GetType();

???

var x = this.GetQueryable< ??? >().ToList();

class Program
{
    static void Main(string[] args)
    {
        var acc = new User();
        acc.Select();
    }
}

public partial class User
{
    public DB_Test001Entities context;

    public User()
    {
        context = new DB_Test001Entities();
    }
    public void Select()
    {  
        Type type = this.GetType();
        var x = this.GetQueryable< **???** >().ToList();
    }

    public IQueryable<TEntity> GetQueryable<TEntity>(List<string> includes = null) where TEntity : class
    {
        IQueryable<TEntity> items = context.Set<TEntity>();

        if (includes != null && includes.Any())
            includes.Where(i => i != null).ToList().ForEach(i => { items = items.Include(i); });

        return items;
    }
}
Community
  • 1
  • 1
ArMaN
  • 2,306
  • 4
  • 33
  • 55

1 Answers1

3

You can do it using reflection. The following sample works smoothly. In program you can use Clerk or Manager, just any instance derived from User to call Select. You can improve your program with this.

    class Clerk : User { }

    class Manager : User { }

    internal class User
    {
        public User() { }

        public string Name { get; set; }

        public void Select()
        {
            var list = new List<string>() {"Jack", "Martin"};
            Type thisType = GetType();
            MethodInfo method = thisType.GetMethod("GetQueryable").MakeGenericMethod(thisType);
            method.Invoke(this, new object[] {list});
        }

        public IQueryable<TEntity> GetQueryable<TEntity>(List<string> includes = null) where TEntity : User, new()
        {
            if(includes != null)
            {
                Console.WriteLine(typeof(TEntity));
                var entity = new List<TEntity>(includes.Count);
                entity.AddRange(includes.Select(item => new TEntity {Name = item}));
                return entity.AsQueryable();
            }
            return null;
        }
    }

    class Program
    {
        static void Main()
        {
            User usr = new Manager();
            usr.Select();
        }
    }
timur
  • 14,239
  • 2
  • 11
  • 32
Kajal Sinha
  • 1,565
  • 11
  • 20
  • tnx a lot. when i use invoke it returns object type, but i want returns IQueryable type. how can i convert it to IQueryable when i haven't TEntity? – ArMaN Jun 26 '14 at 05:10
  • 1
    For an invoke, it is the developer who has to type cast it. You might need to make use of some more reflection here after invoke. Also, you must specify what you wish to do after Invoke, if you are returning TEntity then you must change the Select() method signature to accept generic parameter of type TEntity which again discards any use of invoke as you can directly call the GetQueryable method. This should bubble up to the topmost caller in general scenario when using generics. – Kajal Sinha Jun 26 '14 at 05:15