title: C# Passing generics argument type at runtime
body: <p>In my application I have components that are linked to eachother using inputs and outputs. The data which will be exchanged can be of any type, so I use generics.</p>
  
  <pre><code>public interface IInputVariable&lt;T&gt;
  {
      IOutputVariable&lt;T&gt; Source { get; set; }
  }
  
  
  public interface IOutputVariable&lt;T&gt;
  {
      T Value { get; set; }
  }
  </code></pre>
  
  <p>In another class, the Components are linked together. Which components are linked, are derived from a file. The class which does the linking has no knowledge about which type the input and output exchange. This class only wants to connect them with the following code:</p>
  
  <pre><code>IOutputVariable&lt;double&gt; output;
  Type argumentType = output.GetType().GetGenericArguments()[0];
  IInputVariable&lt;argumentType&gt; input = new BasicInputVariable&lt;argumentType&gt;();
  input.Source = output;
  </code></pre>
  
  <p>This code doesn't compile because argumentType can not be used as an generic argument. Is there a correct way to formulate this? Or is it possible to declare a generic variable without knowing its argument type?</p>