Ruggestion regarding reflection and dynamic loading of controls

  • Thread starter Thread starter bz
  • Start date Start date
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
 
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
 
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 -
 
Back
Top