how can I inspect programatically, in my app, what classes are
implemented into a dll? I need to know what classes are, and if they
implement a specific interface, I need to be able to load that class
dynamically
E.g. someone creates a dll which implements a certain interface
defined in my framework, and put it into a folder of my app.
then I detect that dll was copied there (with filesystemwatcher) and
if the dll contains a class that implements my interface, to be able
to load it.
can anyone point me into right direction to accomplish this?
Thanks
with System.Reflection you can analyse your Assembly and get all
includes Types:
System.Type[] types =
System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
Then you can check, wich type in the array implement the needed
interface and then create an instance of it:
foreach (System.Type t in types)
{
if (t is IMyInterface)
{
IMyInterface obj =
System.Activator.CreateInstance(t);
}
}
Hope it helps,
Sascha
ok, my first idea is not the way it works. Here is the better way:
static void GetModules()
{
foreach (System.Type t in
System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
{
try
{
System.Reflection.TypeFilter myFilter = new
System.Reflection.TypeFilter(MyInterfaceFilter);
Type[] myInterfaces =
t.FindInterfaces(myFilter,typeof(IModule).FullName);
if (myInterfaces.Length > 0)
{
IModule m = System.Activator.CreateInstance(t)
as IModule;
if (m != null)
{
//Here it is ;-)
}
}
}
catch { }
}
}
public static bool MyInterfaceFilter(Type typeObj,Object
criteriaObj)
{
if(typeObj.ToString() == criteriaObj.ToString())
return true;
else
return false;
}
Hope it works for you,
Sascha