Reflection and Interfaces

G

Guest

Hello All,

I'm dynamically loading a form through reflection. I have this working.
What I'd like to do is implement an interface so that I can cast my instance
to that interface. I want to be able to call methods and pass parameters
normally in the dynamically loaded Form.

Yesterday Nicholas Paldino's suggested this. Btw, thanks Nick! As hard as
I tried without asking further questions, I need a little more help. :)

Ok, here is a tiny bit of my buggered code. Inside this namespace I added
an interface at the top of the class. I didn't implement the interface in
the MDI. I added the same interface code in the top of the child form (which
gets compiled into a class lib) and in this class lib I actually implement
the interface.

I'm really new with interfaces and reflection, so any and all suggestions
are very welcome.

Thanks very much in advance for your time.
John F

MDI App:
--------------
namespace DynamicLoadMDI
{
public interface IToken
{
string passParams(Object objTemp);
}

public class Form1 : System.Windows.Forms.Form
{
..
..
..

private void btnLoad_Click(object sender, System.EventArgs e)
{
DynamicFormClass objFormToLoad = colAvailableForms[0] as DynamicFormClass;

Assembly assem = Assembly.LoadFrom(objFormToLoad.Location);

Type[] types = assem.GetTypes();

Object genericInstance = assem.CreateInstance(types[1].FullName);

//IToken tok = genericInstance as types[0].GetType(); // I tried this, but
it's always a null reference.

Form tmpForm = genericInstance as Form;
tmpForm.MdiParent=this;
tmpForm.Show();

}

private void Form1_Load(object sender, System.EventArgs e)
{
this.colAvailableForms = new ArrayList();
Object objNewForm = new DynamicFormClass("c:\\testform.dll","Patient
Selection");
colAvailableForms.Add(objNewForm);
}

Child DLL
-----------------
namespace WindowsApplication4
{
interface IToken
{
string passParams(Object objTemp);
}

public class Form1 : System.Windows.Forms.Form, IToken
{
..
..
..
public string passParams(Object objTemp)
{
..
..
..
}
 
A

Adam Clauss

John F said:
Hello All,

I'm dynamically loading a form through reflection. I have this working.
What I'd like to do is implement an interface so that I can cast my
instance
to that interface. I want to be able to call methods and pass parameters
normally in the dynamically loaded Form.
Ok, here is a tiny bit of my buggered code. Inside this namespace I added
an interface at the top of the class. I didn't implement the interface in
the MDI. I added the same interface code in the top of the child form
(which
gets compiled into a class lib) and in this class lib I actually implement
the interface.
So you have the interface declared (in code) twice? That is going to be a
problem.

Both the Form that implements the interface and the location where you USE
the interface must reference the SAME class - not two interfaces that happen
to have the same name. In this case, they are not even in the same
namespace (DynamicLoadMDI.IToken and WindowsApplication4.ITokeN).

If you cannot add a reference to the Child DLL in your main app, then the
interface should be declared in some third DLL. Then, rather than
containing the interface definition in either your MDI app or your child
DLL, have both of those projects reference the third DLL that contains the
interface code.
 

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