Ruggestion regarding reflection and dynamic loading of controls

B

bz

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
 
R

Ravi Bhavnani

This is easily done using reflection. This example loads the first
module in the assembly that's derived from MyType and (if loaded),
invokes its DoSomething() method.

Assembly asm = Assembly.LoadFrom (addInFilename);
MyType myType = null;
foreach (Type t in types) {
Type baseType = t.BaseType;
if (t.IsPublic && (baseType != null) && baseType.Equals (typeof (MyType)))
{
ConstructorInfo ctor = t.GetConstructor (Type.EmptyTypes);
Object module = ctor.Invoke (new object[] {});
myType = module as MyType;
break;
}
}

if (myType != null)
myType.DoSomething();

Hope this helps!

/ravi
 
B

bz

Thank you
It was exactly what I was looking for

Best regards

This is easily done using reflection.  This example loads the first
module in the assembly that's derived from MyType and (if loaded),
invokes its DoSomething() method.

Assembly asm = Assembly.LoadFrom (addInFilename);
MyType myType = null;
foreach (Type t in types) {
  Type baseType = t.BaseType;
  if (t.IsPublic && (baseType != null) && baseType.Equals (typeof (MyType)))
{
      ConstructorInfo ctor = t.GetConstructor (Type.EmptyTypes);
      Object module = ctor.Invoke (new object[] {});
      myType = module as MyType;
      break;
  }

}

if (myType != null)
    myType.DoSomething();

Hope this helps!

/ravi




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- Ascunde citatul -

- Afiºare text în citat -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top