471,306 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Pass param to an invoked method

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;}
}
}
}
Dec 12 '05 #1
1 1412
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]
- mv*@spam.guard.caspershouse.com

"John F" <jf@rt.com> wrote in message
news:4C**********************************@microsof t.com...
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;}
}
}
}

Dec 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by Radde | last post: by
4 posts views Thread by z_learning_tester | last post: by
reply views Thread by rosydwin | 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.