title: How to get MethodInfo for a generic method by name and its generic parameters?
body: <p>I have this interface:</p>
  
  <pre><code>interface IRepository
  {
      string GetId&lt;T&gt;(T obj) where T : IDomainObject;
      string GetId&lt;T&gt;(Reference&lt;T&gt; reference) where T : IDomainObject;
  }
  </code></pre>
  
  <p>What exactly <code>IDomainObject</code> and <code>Reference</code> are is not relevant to this question. So assume they are completely empty:</p>
  
  <pre><code>interface IDomainObject
  {
      // something
  }
  
  class Reference&lt;T&gt; where T : IDomainObject
  {
      // something
  }
  </code></pre>
  
  <p>My question is: <strong>How do I get the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodinfo" rel="nofollow noreferrer"><code>MethodInfo</code></a> for the <code>GetId&lt;T&gt;</code> method in <code>IRepository</code> that accepts a <code>Reference&lt;T&gt;</code>?</strong></p>
  
  <hr>
  
  <p>Here's what I've tried:</p>
  
  <pre><code>public MethodInfo GetReferenceAcceptingGetIdMethod()
  {
      // We want to return the MethodInfo for the GetId&lt;T&gt; method of IRepository
      // that accepts a Reference&lt;T&gt; argument.
  
      var repositoryInterfaceType = typeof(IRepository);
  
      // Look through all methods of IRepository...
      foreach (var m in repositoryInterfaceType.GetMethods())
      {
          // ... to find a candidate method, going by Genericness and Name ...
          if (m.IsGenericMethodDefinition &amp;&amp; m.Name == nameof(IRepository.GetId))
          {
              // ... and to narrow it further down by looking at the parameters ...
              var parameters = m.GetParameters();
              if (parameters.Length == 1)
              {
                  // ... to check if the one and only parameter is a generic Reference&lt;&gt;.
                  var firstParamType = parameters[0].ParameterType;
                  var genericReferenceType = typeof(Reference&lt;&gt;);
                  if (firstParamType == genericReferenceType)
                  {
                      // !!! This if will never be true.
                      // Why?
                      // And what do I have to change the condition into to make it work?
                      return m;
                  }
              }
          }
      }
  
      throw new Exception();
  }
  </code></pre>
  
  <p>It appears that method's parameter type is somehow different from a completely open generic type. I guess and it seems that the type of the method's parameter is somehow linked to the method's generic type parameter.</p>
  
  <p>So how can I get the <code>MethodInfo</code> in such a case?</p>