我有一个存储库:
public interface IRepository<TEntity> : ICommandRepository<TEntity>, IBaseRepository, IDisposable, IQueryableRepository<TEntity>
{
}
public interface ICommandRepository<TEntity> : IBaseRepository, IDisposable
{
void Delete(Expression<Func<TEntity, bool>> predicate);
}
如果我在运行时知道“ TEntity”的类型,我的目标是使用Lamda Expresion Params调用Delete方法。 例如,我怎么称呼这个方法:
public JsonResult DeleteRows(int[] ids)
{
var repo = DependencyResolver.Current.GetService<IRepository<WORKINGHOURS>>();
repo.Delete(e => ids.Contains(e.Id));
repo.UnitOfWork.Commit();
}
现在,我想在运行时使用反射和类型调用相同的方法:
public JsonResult DeleteRows(string type, int[] ids)
{
Type dbObjectType = Type.GetType(type);
Type repositoryType = typeof (IRepository<>).MakeGenericType(dbObjectType);
var repositoryInstance = ServiceLocator.CurrentLifetimeScope.Resolve(repositoryType);
MethodInfo deleteMethod = repositoryType.GetMethod("Delete");
// TODO:
// Expression<Func<WORKINGHOURS, bool>> searchQuery = p => ids.Contains(p.Id);
var delete = deleteMethod.Invoke(repositoryInstance, .....);
// Commit
}
谢谢!