vendredi 30 juillet 2021

C# Reflection - Getting all types that implement interface and applying them by class type?

I am getting all instances of classes that implement an interface but I need to pass that class type into a Type field and I keep getting "x is a variable but used like a type". How can I get these classes as their type?

Here is my code that pulls in what implements the Interface ICacheData:

private IEnumerable<Type> GetCachables()
{
    var type = typeof(ICacheData);
    var types = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(s => s.GetTypes())
        .Where(p => type.IsAssignableFrom(p));
    return types;
}

But when trying to apply them into this call, I get an error.

var cacheables = GetCachables();
foreach(var cacheable in cacheables)
{
       var cacheItems = LoadAllFromCacheAsync<cacheable>(db, "");
}

LoadAllFromCacheAsync is defined as:

protected static async Task<List<T>> LoadAllFromCacheAsync<T>(DbEntities db, string eagerLoadProps = "") where T : class, ICacheData




Aucun commentaire:

Enregistrer un commentaire