title: C# Func<Interface> with polymorphism
body: <p>I'm trying to implement the visitor pattern for my data structure, which is based on a class hierarchy.
  In C# you can't switch on types (yet). I was thinking about doing something like this as a replacement:</p>
  
  <pre><code>public MyAlgorithm : Func&lt;IBase, X&gt; {
      // default:
      public X apply(IBase data) {}
  
      // case param1 is ConcreteSubclass
      public X apply(ConcreteSubclass data) {}
  
      // case param1 is ConcreteOtherClass
      public X apply(ConcreteOtherClass data) {}
  }
  </code></pre>
  
  <p>And then calling it with late bound dispatch:</p>
  
  <pre><code>public ICanApplyAlgorithmOn {
      public void apply(Func&lt;IBase&gt; alg);
      public TResult apply&lt;TResult&gt;(Func&lt;IBase,TResult&gt; alg);
      public TResult apply&lt;TParam1,TResult&gt;(Func&lt;IBase, TParam1, TResult&gt; alg);
      // etc.
  }
  
  public abstract Base : ICanApplyAlgorithmOn {
      // etc.
      public TResult apply(Func&lt;IBase, X&gt; alg) {
          // Hopefully I am correct in deducing that this will call the various apply(...) implementations instead of always method(IBase)
          dynamic me = this;
          return alg(me);
      }
      // etc.
  }
  </code></pre>
  
  <p>However, this won't work, since <code>MyAlgorithm</code> can't inherit from the delegate <code>Func&lt;...&gt;</code>.</p>
  
  <p>The only solution I saw is to define a lot of interfaces of my own, like the following. Is there a better way?</p>
  
  <pre><code>public interface IAlgorithm { public void apply(IBase data); }
  public interface IAlgorithm&lt;TReturn&gt; { public TReturn apply(IBase data); }
  public interface IAlgorithm&lt;TP1, TReturn&gt; { public TReturn apply(IBase data, TP1 param1); }
  // etc.
  </code></pre>