title: Return an (Anonymous Type with a Function) from a Function - Is There a Better Way?
body: <p>Just so it's known, this question is <em>mostly</em> academic, even though I tried to use the concept in a real-world solution. I realize the example is contrived, but I believe the concept is valid.</p>
  
  <p>I want to write some fluent code like this:</p>
  
  <pre><code>copy(my_first_file).to(my_second_file)
  </code></pre>
  
  <p>Short, easy to read and understand, perfectly legit. So I define my copy method like this:</p>
  
  <pre><code>Private Function copy(FilePath as String) As Object
    Return New With {.to = New Action(Of String)(Function(x) my_real_copy_method(FilePath,x))}
  End Function
  </code></pre>
  
  <p>I realize that I can't force an anonymous type into a specific type (like implementing an interface or some other class), and I don't want the overhead of defining a specific class just to match my desired fluent name with the actual method name. So I was able to make the code work like this:</p>
  
  <pre><code>copy(my_first_file).to.Invoke(my_second_file)
  </code></pre>
  
  <p>So there is no IntelliSense or type awareness there, and I have to include the Invoke in order to have the method run. How can I get more type safety and exclude the Invoke method, under these constraints:</p>
  
  <ul>
  <li>Anonymous Type returned from Method</li>
  <li>No additional classes or interfaces</li>
  <li>Preferably, I do not want to pass in another parameter to the copy() method that tells what type to return, unless copy becomes a generic method (but I think that means defining another class/interface, which I don't want to do)</li>
  </ul>
  
  <p>I know that sounds pretty demanding, feel free to call "Bull" if I'm being to difficult!</p>
  
  <p>Thanks in advance.</p>