lundi 18 mai 2020

C# How to use GetMethod to find an overload with a generic out parameter?

everything is in the title, but let me put some code :

/// <summary>
/// The first version
/// </summary>
public TItem Foo(long param1) { ... }
/// <summary>
/// Overload with a generic parameters
/// </summary>
public bool Foo<TItem>(out TItem item, long param1) { ... }

The goal is:

  • to end up with a MethodInfo for the second 'Foo' method
  • to NOT use GetMethods or GetMember because it's inelegant and prone-to-future-errors
  • to NOT rename any of those methods. The easy GetMethod("name") cannot be used here...

Please note that:

  • The type or TItem is obviously unknown.
  • That MethodInfo is a GenericMethodDefinition, meaning it will need a MakeGenericMethod(...) to be invokable.
  • I did not find the right parameters for GetMethod in the MS documentation.
  • I spent 1 hour searching StackOverflow for the answer without success, but reflection evolved so much that there is a lot of irrelevant answers all around. Adding a date filter in stackoverflow might be a great idea (or maybe a .Net framework version filter ?)
  • You are more than welcome to prove me wrong an find me the obvious answer I missed !
  • My name is Mose and I'm a bullet addict ^^




Aucun commentaire:

Enregistrer un commentaire