using System.Collections;
using System.Collections.Generic;
public class DbSet<T> : List<T>
public string Name { get; set; }
public override string ToString()
public decimal Amount { get; set; }
public override string ToString()
return "Transaction " + Amount.ToString("0.00");
public class DatabaseContext
public DbSet<User> GetUsers()
new User { Name = "Bob" },
new User { Name = "Alice" }
public DbSet<Transaction> GetTransactions()
return new DbSet<Transaction>()
new Transaction { Amount = 12.34M },
new Transaction { Amount = 56.78M }
private readonly DatabaseContext _db;
public HelperClass(DatabaseContext db)
public DbSet<T> GetDataSource<T>()
var targetType = typeof(DbSet<T>);
.Where( m => m.ReturnType == targetType)
.Invoke(_db, null) as DbSet<T>;
public IEnumerable GetDataSource(Type type)
var targetType = typeof(DbSet<>).MakeGenericType(new Type[] { type});
.Where( m => m.ReturnType == targetType)
.Invoke(_db, null) as IEnumerable;
public static void Main()
var helperClass = new HelperClass(new DatabaseContext());
foreach (var u in helperClass.GetDataSource<User>())
foreach (var t in helperClass.GetDataSource(typeof(Transaction)))