I want to call the constructor via a .Net reflection that takes an interface as a parameter. The code for this class looks something like this:
public interface IStringGetter { string GetString( ); } public class Class1 { private IStringGetter _stringGetter; public Class1( IStringGetter stringGetter ) { _stringGetter = stringGetter; } public String GetString( ) { return _stringGetter.GetString( ); } }
The code for using this class with reflection is as follows:
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 } );
And now we need an object that implements the IStringGetter interface. I can not get the object with reflection, because nothing in the library implements the interface. Is there a way to create an object that implements the interface and pass it to the constructor?
I am currently using Windows Forms with Visual Studio 2008, this is a C # project that targets .Net2.0 infrastructure. But I am glad to make any decision.
Edit : Sorry, I did not indicate the problem in full context. Two pieces of code are in different assemblies. The assembly containing the second code snippet does not have a link to the first dll, it just loads the assembly with reflection. If I just write
public class MyStringGetter : IStringGetter
the compiler throws an error because IStringGetter is not known at compile time.
Edit2 . Although this is not what I was hoping for, I think the answer is: Do not do this
source share