title: How can I make C# Create.Delegate support inheritance?
body: <p>I have the following type hierarchy:</p>
  
  <pre><code>public abstract class Parent { }
  public class Child : Parent
  {
      public Task SayAsync(string arg)
      {
          Console.WriteLine(arg);
          return Task.CompletedTask;
      }    
  }
  </code></pre>
  
  <p>I need to achieve the following: </p>
  
  <ul>
  <li>Create any instance of <code>Parent</code> at run-time (this is already solved by calling a <code>Func&lt;Parent&gt;</code> which I obtain elsewhere in the code)</li>
  <li>Then invoke (on that instance) all the <code>public</code> methods (which always return a <code>Task</code> and accept a <code>string</code> as a parameter) passing in the <code>arg</code> which is not a constant.</li>
  </ul>
  
  <p>The above exists in a hot-path therefore in order to improve performance I am resorting to <code>Cached Delegates</code> so at startup I will be creating the delegates which I will then cache and use when required.</p>
  
  <p>Here is an example I have done explicitly for <code>Child</code> which works but I cannot figure out how to make the delegate accept a <code>Parent</code> (as I won't know the type at compile time).</p>
  
  <pre><code>// If I change Child to Parent, I get "Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type"
  private delegate Task Invoker(Child instance, string arg);
  
  void Main()
  {
      var instance = new Child(); // This will be obtained by calling a Func&lt;Parent&gt;
      var methodWithArg = instance.GetType().GetMethod("SayAsync");
  
      var func = GetDelegateWithArg(methodWithArg);
  
      func(instance, "Foo");
  }
  
  private static Invoker GetDelegateWithArg(MethodInfo method)
  {
      object pointer = null;
      return (Invoker)Delegate.CreateDelegate(typeof(Invoker), pointer, method);
  }
  </code></pre>
  
  <p>Any ideas or alternatives to help me achieve the goal is appreciated.</p>