Pass param to an invoked method

G

Guest

Hello,

I'm dynamically loading a child form saved in a DLL. I have this code
working and it's pretty straightforward.

What I'd like to do is pass an object type to an invoked method in the child
form as soon as it's loaded. It seems I can get the invoke working just
fine, but I can't get the object successfully cast inside the invoked method.

Thanks in advance,
John F.

Here is my code:
// Calling code from within my MDI app
---------------------------------------------------
// Global Member
ArrayList colAvailableForms;

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[0].FullName);

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

// array with one member
Type[] paramTypes = new Type[1];

paramTypes[0]= Type.GetType("System.Object");

// Get method info for passParams()
MethodInfo passParam = types[0].GetMethod("passParams",paramTypes);

// fill an array with the actual parameters
Object[] parameters = new Object[1];

parameters[0] = colAvailableForms[0];

try
{
Object returnVal = passParam.Invoke(null,parameters);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

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


// Child form. Code inside of testform.dll
------------------------------------------
static public void passParams(Object objTemp)
{
//I've tried this and get an exception.
//DynamicLoadMDI.DynamicFormClass objFormToLoad =
(DynamicLoadMDI.DynamicFormClass) objTemp;

// Produces null.
DynamicLoadMDI.DynamicFormClass objFormToLoad = objTemp as
DynamicLoadMDI.DynamicFormClass;

MessageBox.Show(objFormToLoad.Location.ToString());
}


// DynamicFormClass that's defined in both MDI and DLL.
-------------------------------------------------------
namespace DynamicLoadMDI
{
public class DynamicFormClass
{
private string _strLocation;
private string _strDescription;

public DynamicFormClass(string strLocation, string strDescription)
{
this._strLocation = strLocation;
this._strDescription = strDescription;
}

public string Location
{
get {return _strLocation;}
set {_strLocation = value;}
}

public string Description
{
get {return _strDescription;}
set {_strDescription = value;}
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

John,

Why not have your form implement an interface and then cast that
instance to that interface? Then, you can call methods normally, and not
have to worry about all of the reflection.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John F said:
Hello,

I'm dynamically loading a child form saved in a DLL. I have this code
working and it's pretty straightforward.

What I'd like to do is pass an object type to an invoked method in the
child
form as soon as it's loaded. It seems I can get the invoke working just
fine, but I can't get the object successfully cast inside the invoked
method.

Thanks in advance,
John F.

Here is my code:
// Calling code from within my MDI app
---------------------------------------------------
// Global Member
ArrayList colAvailableForms;

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[0].FullName);

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

// array with one member
Type[] paramTypes = new Type[1];

paramTypes[0]= Type.GetType("System.Object");

// Get method info for passParams()
MethodInfo passParam = types[0].GetMethod("passParams",paramTypes);

// fill an array with the actual parameters
Object[] parameters = new Object[1];

parameters[0] = colAvailableForms[0];

try
{
Object returnVal = passParam.Invoke(null,parameters);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

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


// Child form. Code inside of testform.dll
------------------------------------------
static public void passParams(Object objTemp)
{
//I've tried this and get an exception.
//DynamicLoadMDI.DynamicFormClass objFormToLoad =
(DynamicLoadMDI.DynamicFormClass) objTemp;

// Produces null.
DynamicLoadMDI.DynamicFormClass objFormToLoad = objTemp as
DynamicLoadMDI.DynamicFormClass;

MessageBox.Show(objFormToLoad.Location.ToString());
}


// DynamicFormClass that's defined in both MDI and DLL.
-------------------------------------------------------
namespace DynamicLoadMDI
{
public class DynamicFormClass
{
private string _strLocation;
private string _strDescription;

public DynamicFormClass(string strLocation, string strDescription)
{
this._strLocation = strLocation;
this._strDescription = strDescription;
}

public string Location
{
get {return _strLocation;}
set {_strLocation = value;}
}

public string Description
{
get {return _strDescription;}
set {_strDescription = value;}
}
}
}
 

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