mercredi 22 février 2023

Call a method on any field of a specific GENERIC type c#

I have the exact same problem as the OP of this thread, except the type I am trying to filter is a generic type and I have no beforehand information of the used type.

Let say the type is: MyGenericType<T>. what I would like to do is:

var fields = this.GetType().GetFields(BindingFlags.NonPublic | 
                            BindingFlags.Instance).Where(x=>x.FieldType == typeof(MyGenericType<>));

However this will not work as at runtime a new type, that contains the information about the generic type, is generated.

Is there a way to circumvent this problem ?





Aucun commentaire:

Enregistrer un commentaire