mardi 1 novembre 2022

Using reflection is it possible to get a collection of types by namespace then use them in a generic system?

To be clear, I'm not looking for "How do you get a type from an object", I'm asking how to use a generic Type to also create a generic class.

Using the below code I'm looking to reference my classes within the "Foo" namespace and be able to use them again in my generic system:

var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "Foo", StringComparison.Ordinal)).ToArray();

What I would like to be able to do, is:

public class Bar<T>
{
   ...
}

... 

foreach (Type T in types)
{
    //Create a generic Bar with T
    //Something like new Bar<T>();
}

Is the current way I'm going about it the proper way and if so what would be the best way to handle Bar in this case?





Aucun commentaire:

Enregistrer un commentaire