title: .Net Reflection: How to invoke a constructor that takes a interface as parameter
body: <p>I want to invoke a constructor via .Net reflection that takes a interface as a parameter. The code for this class looks something like this:</p>
  
  <pre><code>public interface IStringGetter
  {
      string GetString( );
  }
  
  public class Class1
  {
      private IStringGetter _stringGetter;
      public Class1( IStringGetter stringGetter )
      { 
          _stringGetter = stringGetter;
      }
  
      public String GetString( )
      {
          return _stringGetter.GetString( );
      }
  }
  </code></pre>
  
  <p>The code for using this class with reflection looks like the following:</p>
  
  <pre><code>  Assembly asm = Assembly.LoadFrom( @"c:\temp\ClassLibrary1.dll" );
    Type tClass1 = asm.GetType( "ClassLibrary1.Class1" );
    Type tStringGetter = asm.GetType( "ClassLibrary1.IStringGetter" );
  
    ConstructorInfo ci = tClass1.GetConstructor( new Type[ ] { tStringGetter } );
    // object obj = ci.Invoke( new object[ ] { *what goes here?* } );
  </code></pre>
  
  <p>And now a need a object that implements the IStringGetter interface. I cannot obtain a object with reflection, because nothing in the library implements the interface. Is there any way to create a object that implements the interface and pass it to the constructor?</p>
  
  <p>Right now I'm using Windows Forms with Visual Studio 2008, it is a C# project that targets the .Net2.0 framework. But I'm happy to accept any solution. </p>
  
  <p><strong>Edit</strong>: Sorry, I did not state the problem in full context. The two code snippets are in different assemblies. The assembly that contains the second code snippet does not have a reference to the first dll, it just loads the assembly with reflection. If I just write </p>
  
  <pre><code>public class MyStringGetter : IStringGetter
  </code></pre>
  
  <p>the compiler throws an error because IStringGetter is not known at compile time.</p>
  
  <p><strong>Edit2</strong>: Although it is not what I hoped for, I think the answer is: <strong><em>Don't do that</em></strong></p>