how to create a generic T object dynamically
Code:
public T GetReposit<T>() where T : class {
var result = default(T);
if (typeof(T) == typeof(UserRepository))
{
result = new UserRepository(Context) as T;
}
if (typeof(T) == typeof(ProductRepository))
{
result = (T)(object)new ProductRepository(Context);
}
if (typeof(T) == typeof(CustomerRepository))
{
result = (T)(object)new CustomerRepository(Context);
}
// etc....
return result;
}
// when i have BaseListPage
public class BaseListPage<TEntity, TRepository> : BasePage
where TEntity : Tentity
where TRepository : BaseRepository<TEntity> {
protected TRepository _repository;
public BaseListPage() {
UnitOfWork unit = UnitOfWork.Instance;
_repository = unit.GetRepository<TRepository>();
}
//it actually works but i want to optimize this code