How to call a generic method using meta.Method.ReturnType as generic parameter? #235
-
Is there a way to use meta.Method.ReturnType as a generic parameter in an aspect (OverrideMethodAspect)? Inspired by your caching sample but using IDistributedCache and serializing/deserializing cached data using System.Text.Json, how can I call the following method where T should be set at compile time using the known ReturnType of the function? I've tried to find examples doing this but without any success:
cachedData in this case is a byte array. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is possible using a generic auxiliary template. See https://doc.metalama.net/conceptual/aspects/templates/auxilliary-templates#dynamic-invocation-of-generic-templates. However, in your case, you should instead use the non-generic overload of You may also like our ready-made caching aspect. See https://doc.metalama.net/patterns/caching. |
Beta Was this translation helpful? Give feedback.
This is possible using a generic auxiliary template. See https://doc.metalama.net/conceptual/aspects/templates/auxilliary-templates#dynamic-invocation-of-generic-templates.
However, in your case, you should instead use the non-generic overload of
JsonSerializer.Deserialize
, this is simpler.You may also like our ready-made caching aspect. See https://doc.metalama.net/patterns/caching.