mercredi 11 mai 2016

Make generic Type on function as Generic Object

I have a function which make repository Dictionary, of multiple tables at run time, What i want is make this as generic, but I am stuck on this error..

 public IRepository<TEntity> GetRepository<TEntity>(string TypeName) where TEntity : class 

 {

        Type t = GetInstance(TypeName);
        if (_repositories.Keys.Contains(t))
        {
            return _repositories[t] as IRepository<TEntity>;
        }

        var repository = new Repository<TEntity>(_context);

        _repositories.Add(t, repository);

        return repository;
 }

When I call this function as

 string tn = tt.GetType().FullName;
 Type tttt = GetInstance(tn);
 uu.GetRepository<Object>(tn).Add(tt);

it throws Exception

The entity type Object is not part of the model for the current context.

how can I make this happen as

string tn = tt.GetType().FullName;
Type tttt = GetInstance(tn);
uu.GetRepository<typeof(tt)>(tn).Add(tt);

or something type else.





Aucun commentaire:

Enregistrer un commentaire