mercredi 18 octobre 2023

Can a closed generic type be activated via reflection?

My question here is related to this question, which has received sufficient attention but no answers/comments so far.

In response to my discovery that t remains an empty List(Of Type) after running RegisterAssemblyTypes() (further down, in my edit), I dug into the Autofac source code a bit. What I found is interesting, especially for this case.

It seems that Autofac considers a closed generic type to be non-registerable, at least when specified at runtime. We find this in the comments of the internal function FilterAndRegisterConcreteTypes():

Filters a list of types down to only those that are concrete, not open generic, and allowed by the provided activator data.

Since t emerges clean and unscathed, it seems that Autofac considers the ZipCodeRepository (Of ZipCode) type (and its cousins) unregisterable when loaded at runtime via reflection. Note that we can register that specific type at design time all day long, like so:

Builder.RegisterType(Of ZipCodeRepository(Of ZipCode))

...but for some unknown reason (to me, at least) it gets dropped when registering via reflection. Curiously, however, it doesn't seem to satisfy the requirements for exclusion.

So why is Autofac's RegisterAssemblyTypes() function filtering out my repositories?





Aucun commentaire:

Enregistrer un commentaire