0

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
Charkins12
  • 190
  • 4
  • 16
  • You are trying to set a generic Function with a variable, C# Generics must know the type in compile time, and you are trying to do it in run time. – Ethan.S Jul 30 '21 at 15:33
  • Ethen is right, and plus : you are not executing the method since it must be awaited. – Romka Jul 30 '21 at 16:11
  • In this code base I found another method that isn't Async. Reflection is very new to me. I have attempted to follow along with what is posted in Johnathan's shared link, but to no avail. Updating original post to see if anyone can glean more. – Charkins12 Aug 01 '21 at 01:56

0 Answers0