title: Is there a way to discover the original generic argument type of a instance of an Action<in T> delegate?
body: <p>Is there a way to find the declared generic argument type of an instance of an Action delegate?</p>
  
  <p>In the following code the <code>WriteGenericArgumentType</code> function expects and instance of <code>Action&lt;String&gt;</code> but because <code>Action&lt;in T&gt;</code> allows for contravariance callers of the function are able to call it with <code>Action&lt;Object&gt;</code>.  </p>
  
  <pre><code>static void Main( )
  {
      WriteGenericArgumentType( new Action&lt;string&gt;( s =&gt; { } ) );
      WriteGenericArgumentType( new Action&lt;object&gt;( o =&gt; { } ) );
  }
  
  static void WriteGenericArgumentType( Action&lt;string&gt; action )
  {
      Console.WriteLine( DiscoverGenericArgumentType( action ).Name );
  }
  
  static Type DiscoverGenericArgumentType( Delegate action )
  {
      return action.GetType().GetGenericArguments()[0];
  }
  </code></pre>
  
  <p>However, I need to be able to discover the generic argument type that was defined in the signature of <code>WriteGenericArgumentType</code> from the instance of the delegate that is passed to <code>DiscoverGenericArgumentType</code>.</p>
  
  <p>When the code runs I get the following output:</p>
  
  <pre><code>String
  Object
  </code></pre>
  
  <p>But I need it to be:</p>
  
  <pre><code>String
  String
  </code></pre>
  
  <p>For my purposes I cannot change the signature of <code>DiscoverGenericArgumentType</code> and I need that function to return <code>string</code> as the type.</p>
  
  <p>I know I can create my own generic delegate type that doesn't allow contravariance but I'm looking for a way to not have to change the signature of the API if possible.</p>
  
  <p>Any ideas?</p>