.NET: Calling GetInterface method of Assembly obj with a generic interface argument
Posted
by Khnle
on Stack Overflow
See other posts from Stack Overflow
or by Khnle
Published on 2010-06-07T17:15:24Z
Indexed on
2010/06/07
17:32 UTC
Read the original article
Hit count: 185
I have the following interface:
public interface PluginInterface<T> where T : MyData
{
List<T> GetTableData();
}
In a separate assembly, I have a class that implements this interface. In fact, all classes that implement this interface are in separate assemblies. The reason is to architect my app as a plugin host, where plugin can be done in the future as long as they implement the above interface and the assembly DLLs are copied to the appropriate folder.
My app discovers the plugins by first loading the assembly and performs the following:
List<PluginInterface<MyData>> Plugins = new List<PluginInterface<MyData>>();
string FileName = ...;//name of the DLL file that contains classes that implement the interface
Assembly Asm = Assembly.LoadFile(Filename);
foreach (Type AsmType in Asm.GetTypes())
{
//Type type = AsmType.GetInterface("PluginInterface", true); //
Type type = AsmType.GetInterface("PluginInterface<T>", true);
if (type != null)
{
PluginInterface<MyData> Plugin = (PluginInterface<MyData>)Activator.CreateInstance(AsmType);
Plugins.Add(Plugin);
}
}
The trouble is because neither line where I am getting the type as by doing Type type = ... seems to work, as both seems to be null. I have the feeling that the generic somehow contributes to the trouble. Do you know why?
© Stack Overflow or respective owner