還有一類創建一個泛型類的類型名稱
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