471,605 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Reflection and Interfaces

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)
{
..
..
..
}


Dec 13 '05 #1
1 2229
"John F" <jf@rt.com> wrote in message
news:44**********************************@microsof t.com...
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.

--
Adam Clauss
Dec 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ichor | last post: by
11 posts views Thread by Ian | last post: by
3 posts views Thread by HL | last post: by
2 posts views Thread by Stefan | last post: by
1 post views Thread by Big Daddy | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.