我已经实现了一个通用资源库,但有一个要求是在运行时动态地创建适当的资源库,并将实体类型设置为字符串。如何投射到MyInterface <TEntity>
我已成功地创建使用存储库:
Assembly common = Assembly.LoadFrom(@"CommonLibrary.dll");
Type entityType = common.GetType("Models.OneOfManyEntities");
Type repoType = typeof(TradeSiftRepository<>).MakeGenericType(entityType);
var repo = Activator.CreateInstance(repoType, new UnitOfWork());
然而repo
是一个对象,我真的想将它转换为IRepository<TEntity>
我已经试过IRepository<entityType>
但这是不正确的
这是接口用于回购:
public interface IRepository<TEntity> : IDisposable where TEntity : class
{
IQueryable<TEntity> FindAll();
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");
TEntity FindById(object Id);
void Insert(TEntity entity);
void Update(TEntity entity);
void Delete(object id);
void Delete(TEntity entity);
}
回购的活化实例是正确的类型,但甲ctivator返回一个对象类型,所以我需要将它转换为使用方法。
你什么类型,如果你使用' repo.GetType()'? – Xharze 2012-07-31 11:12:38
回购的创建实例是正确的类型,但我必须将其转换为使用方法,例如repo.FindById(1) – HadleyHope 2012-07-31 11:15:30