dynamic load assembly, dynamic class

  • Thread starter Thread starter Romain TAILLANDIER
  • Start date Start date
R

Romain TAILLANDIER

hi group

I have an application where i dynamicly load a dll.
this dll contain a class MyClass and an interface IMyClass
my application contain the IMyClass interface too.

[dll : ]
interface IMyClass{...}
class MyClass : IMyClass{...}

[MyApp : ]
interface IMyClass{...}

in my code, i want to create an instance of MyClass.
i use a code like that :

Assembly assem = Assembly.Load(this.DllName);
Type MyType = assem.GetType("MyClass");
Object obj = Activator.CreateInstance(myType);
MethodInfo myMethode = myType.GetMethod("createInstance");
//this 4 lines works !
// MyClass :: createInstace, return a MyClass instance

IMyClass instance = (IMyClasse)myMethode.Invoke(obj,null);

// this last line does'nt work because of the cast , naturally...

I need to do exactly this, but it can't work because the type MyClass is
only define on the dll :
MyClass instance = myMethode.Invoke(obj,null);

I don't see any solution ....
Please save me !!
ROM
 
Thank you john for your respond.
but i have more questions now ....
That's your problem, right there. You should only have IMyClass defined
in one place.

first, if I have do not have many déclaration of IMyClass, where do i define
it ?
then the fact that i have many déclaration was'nt my problem, in fact i
can't creat an instance of MyClass wich is in the DLL. I want to be able to
have it correctly, and be able to call all its method. Must i Invoke them
each time ? it is quite heavy isn't it ?
I don't know how to proceed to use the entiere fonctionnalities of the class
without having to reload the methods each time i need them.

Thank you for helping once more.
ROM
 
Romain TAILLANDIER said:
first, if I have do not have many déclaration of IMyClass, where do i define
it ?

In a separate library assembly which both of the other assemblies
reference.
then the fact that i have many déclaration was'nt my problem, in fact i
can't creat an instance of MyClass wich is in the DLL.

Why not? What happens when you try? From your previous message, it
looked like you *had* managed to create an instance of MyClass.
I want to be able to
have it correctly, and be able to call all its method. Must i Invoke them
each time ? it is quite heavy isn't it ?

No, you define the methods in the interface, cast the reference
returned by Activator.CreateInstance to the interface type, and invoke
methods on that.
I don't know how to proceed to use the entiere fonctionnalities of the class
without having to reload the methods each time i need them.

Look at the source code on the plug-in article page again - it does
everything it looks like you need it to.
 
Sorry for my late respond jon !

I want to thank you for you great help. I found how to do after my second
anwsers series, and i do exactly as you explain.

I think there is to few "thanks you" on this group, and they are so
gratifiant (?? i am not sure of the english word ...), i wish there were
more.

so, THANK YOU jon
ROM


"Jon Skeet" <[email protected]> a écrit dans le message de (e-mail address removed)...
Romain TAILLANDIER said:
first, if I have do not have many déclaration of IMyClass, where do i define
it ?

In a separate library assembly which both of the other assemblies
reference.
then the fact that i have many déclaration was'nt my problem, in fact i
can't creat an instance of MyClass wich is in the DLL.

Why not? What happens when you try? From your previous message, it
looked like you *had* managed to create an instance of MyClass.
I want to be able to
have it correctly, and be able to call all its method. Must i Invoke them
each time ? it is quite heavy isn't it ?

No, you define the methods in the interface, cast the reference
returned by Activator.CreateInstance to the interface type, and invoke
methods on that.
I don't know how to proceed to use the entiere fonctionnalities of the class
without having to reload the methods each time i need them.

Look at the source code on the plug-in article page again - it does
everything it looks like you need it to.
 
Back
Top