0

Why can't I do this?

     services.AddSingleton<Type.GetType("ShoppingCartCache",true)>();

OR is there a better way of passing from a string

LeBlues
  • 305
  • 2
  • 5
  • 20

1 Answers1

0

The method also has an overload to pass a type as parameter. So you can do:

services.AddSingleton(typeof(ShoppingCartCache));

Same also works with your example (although more prone to runtime errors):

services.AddSingleton(Type.GetType("ShoppingCartCache",true));

The reason it doesn't work is because generic types must be static, thus known at compilation.

NotFound
  • 5,005
  • 2
  • 13
  • 33