还有一类创建一个泛型类的类型名称
public class Repository <TKey, TEntity>
{
public ICollection<TEntity> Get()
{
using (var session = NHibernateHelper.OpenSession())
{
if (typeof(TEntity).IsAssignableFrom(typeof(IActualizable)))
return session.CreateCriteria(typeof(TEntity)).Add(Restrictions.Lt("ActiveTo", DBService.GetServerTime())).List<TEntity>();
return session.CreateCriteria(typeof(TEntity)).List<TEntity>();
}
}
}
如何创建它,知道TEntity的唯一名字?
实施例:
类游戏 { }
串nameEntity = “游戏”;
var repository = new Repository < long,???>();
检查此问题http://stackoverflow.com/questions/493490/converting-a-string-to-a-class-name –
Type type = Type.GetType(entityName); 类型entity = typeof(DBCloud.Repositories.Repository <>)。MakeGenericType(new Type [] {type}); var rep = Activator.CreateInstance(entity); 错误\t 1使用泛型类型'Repository'需要2个类型参数 –
ask125342