vendredi 25 octobre 2019

How can I execute a method with type

Sorry for the odd question wording.. I understand the concept but my google skills are lacking today.

I have a method that allows me to specify a generic to use for its work. Any class that gets passed in will inherit a class of "BaseProduct". This allows me to write code like the following.

SyncProductsByType<PublicationProduct>();
SyncProductsByType<ApparelProduct>();
.... etc

I just want to do a foreach on the basetype's subclasses, but I can't get the syntax right. So far, I have:

Type parentType = typeof(BaseProduct);
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] types = assembly.GetTypes();
IEnumerable<Type> subclasses = types.Where(t => t.BaseType == parentType);

but thats ALL WRONG for use in a foreach. I can't figure out how to do this sort of thing:

foreach (<T> subclasse yadda yadda) {
    SyncProductsByType<something?>();
}

Thats not even close. Thanks





Aucun commentaire:

Enregistrer un commentaire