lundi 6 avril 2015

Variable Type inside Diamond Brackets [duplicate]


This question already has an answer here:




My current code looks like the following:



var cacheWork = new UnitOfWork();
Log.Info("Caching Cable Entity");
cacheWork.GetRepository<CableEntity>().All();
Log.Info("Caching CablePart Entity");
cacheWork.GetRepository<CablePartEntity>().All();
Log.Info("Caching CableType Entity");
cacheWork.GetRepository<CableTypeEntity>().All();
Log.Info("Caching Country Entity");
cacheWork.GetRepository<CountryEntity>().All();
Log.Info("Caching Filesystem Entity");
cacheWork.GetRepository<FilesystemEntity>().All();
Log.Info("Caching Host Entity");
cacheWork.GetRepository<HostEntity>().All();
Log.Info("Caching Location Entity");
cacheWork.GetRepository<LocationEntity>().All();
Log.Info("Caching NodeCable Entity");
cacheWork.GetRepository<NodeCableEntity>().All();
Log.Info("Caching Node Entity");
cacheWork.GetRepository<NodeEntity>().All();
Log.Info("Caching Person Entity");
cacheWork.GetRepository<PersonEntity>().All();
Log.Info("Caching Tier Entity");
cacheWork.GetRepository<TierEntity>().All();
Log.Info("Caching User Entity");
cacheWork.GetRepository<UserEntity>().All();
cacheWork.Dispose();


What i want is to get rid of the explicit declaring of type for each of these classes. I am able to retrieve all of the types and return it as a list.


What i now want is to do something like this:



var ns = typeof(T).Namespace;
var types = typeof(T).Assembly.GetExportedTypes()
.Where(t => t.Namespace == ns);

var cacheWork = new UnitOfWork();
foreach (Type t in types)
cacheWork.GetRepository<t>().All();
cacheWork.Dispose();


So i already know that that this syntax does not work, But i'm figuring it has to be a way of doing this via for example reflection. Anyone That can help me with this?






Aucun commentaire:

Enregistrer un commentaire