title: C# Generic Method vs Casting
body: <p>I'm writing a program in C# (3.5 at the moment, but likely to be adaptable to other versions as the need arises) that uses a simple plugin architecture to control input and output. Each plugin is a DLL that is loaded when the user chooses the plugins to use.</p>
  
  <p>Since the actual plugin class isn't known until run time, I'm using reflection in a wrapper class to call methods and access properties of the plugins.</p>
  
  <p>Up until now, I've been using the following to call methods on a plugin:</p>
  
  <pre><code>public object Method(string methodName, params object[] arguments) {
    // Assumed variables/methods/exceptions:
    //   Dictionary&lt;string, MethodInfo&gt; Methods: a cache of MethodInfo's
    //     of previously called methods.
    //   NoSuchMethodException: thrown if an unknown/unreachable method is
    //     requested. The message member contains the invalid method name
    //   void LoadMethod(string methodName, params object[] arguments): responsible
    //     for retrieving the MethodInfo's, or throw a NoSuchMethodException
    //   object Plugin: an instance of the dynamically loaded class.
    if (!Methods.ContainsKey(methodName)) {
      LoadMethod(methodName, arguments);
    }
    if (arguments != null &amp;&amp; arguments.Length == 0) {
      arguments = null;
    }
    return Methods[methodName].Invoke(Plugin, arguments);
  }
  </code></pre>
  
  <p>Which is used like:</p>
  
  <pre><code>string[] headers = (string[]) Plugin.Method("GetHeaders", dbName, tableName);
  </code></pre>
  
  <p>This works nicely, as long as the caller correctly casts the return value to the expected
  type. Plugins must implement certain interfaces, so the caller should know this type.</p>
  
  <p>After doing some further work with reflection however, the following alternate form occurred to me:</p>
  
  <pre><code>public T Method&lt;T&gt;(string methodName, params object[] arguments) {
    if (!Methods.ContainsKey(methodName)) {
      LoadMethod(methodName, arguments);
    }
    if (Methods[methodName].ReturnType != typeof(T)) {
      // Could also move this into LoadMethod to keep all the throwing in one place
      throw new NoSuchMethodException(methodName);
    }
    if (arguments != null &amp;&amp; arguments.Length == 0) {
      arguments = null;
    }
    return (T) Methods[methodName].Invoke(Plugin, arguments);
  }
  </code></pre>
  
  <p>This one is used like:</p>
  
  <pre><code>string[] headers = Plugin.Method&lt;string[]&gt;("GetHeaders", dbName, tableName);
  </code></pre>
  
  <p>This version essentially moves the casting into the Method method. The caller obviously still needs to know the expected return type, but that was always going to be the case. It doesn't work for void methods, but I can include a version of Method for that:</p>
  
  <pre><code>public void Method(string methodName, params object[] arguments) {
    // Good for void methods, or when you're going to throw away the return
    // value anyway.
    if (!Methods.ContainsKey(methodName)) {
      LoadMethod(methodName, arguments);
    }
    if (arguments != null &amp;&amp; arguments.Length == 0) {
      arguments = null;
    }
    Methods[methodName].Invoke(Plugin, arguments);
  }
  </code></pre>
  
  <p>My question is - is one of these intrinsically better than the other (for a given value of 'better')? For instance, is one notably faster? Easier to understand? More supported?</p>
  
  <p>I personally like the look of the latter, although I am a bit worried that my return type testing (<code>Methods[methodName].ReturnType != typeof(T)</code>) might be too simplistic. Interestingly, it was originally <code>!(Methods[methodName].ReturnType is T)</code>, but that always seemed to fail.</p>
  
  <p>The closest existing question to this I could find was <a href="https://stackoverflow.com/questions/6795402/generic-method-to-type-casting">Generic method to type casting</a>, and some of the answers suggested that the latter method is more expensive than the former, but there's not much in the way of detail there (the question there is more towards implementation of the method rather than which is better).</p>
  
  <p><strong>Clarification:</strong> This is a hand-rolled, very limited plugin system, not using IPlugin. I'm more interested in the question of whether generic methods are inherently better/worse than expecting the caller to cast in this situation.</p>