Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Suggestion regarding reflexion and dynamic load of controls

0 views
Skip to first unread message

bz

unread,
Feb 5, 2008, 1:09:52 PM2/5/08
to
Hi,

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


Sascha Folville

unread,
Feb 12, 2008, 5:54:10 AM2/12/08
to
Hi bz,

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

Sascha Folville

unread,
Feb 13, 2008, 3:42:33 AM2/13/08
to
Hi again,

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

0 new messages