Dynamically create a generic using reflection

A long time ago I created a class which helps create a sut for unit testing. This class is generic and has the type of the sut as a generic parameter. Details see ContextFor<> zum Testen des SUT.

Now, I was trying to use the `ContextFor<T>` in a data-driven test which passes the type as a parameter. This is a little tricky because the generic class is not known during compile time. The casting is limited due to the covariance of the generic parameter `T. Besides, the default Activator.CreateInstance` is not designed for dynamic generics.

The following snippet shows the creation of a generic with a dynamic generic parameter `T`. The `dynamic` can be replaced by `object` but obviously not to the data-driven parameter.


Posted

in

Kommentare

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert