473,663 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 colAvailableFor ms;

private void btnLoad_Click(o bject sender, System.EventArg s e)
{
DynamicFormClas s objFormToLoad = colAvailableFor ms[0] as DynamicFormClas s;

Assembly assem = Assembly.LoadFr om(objFormToLoa d.Location);

Type[] types = assem.GetTypes( );

Object genericInstance = assem.CreateIns tance(types[0].FullName);

Form tmpForm = genericInstance as Form;
tmpForm.MdiPare nt=this;
tmpForm.Show();

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

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

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

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

parameters[0] = colAvailableFor ms[0];

try
{
Object returnVal = passParam.Invok e(null,paramete rs);
}
catch(Exception ex)
{
MessageBox.Show (ex.ToString()) ;
}
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
this.colAvailab leForms = new ArrayList();
Object objNewForm = new DynamicFormClas s("c:\\testform .dll","Descript ion");
colAvailableFor ms.Add(objNewFo rm);
}
// Child form. Code inside of testform.dll
------------------------------------------
static public void passParams(Obje ct objTemp)
{
//I've tried this and get an exception.
//DynamicLoadMDI. DynamicFormClas s objFormToLoad =
(DynamicLoadMDI .DynamicFormCla ss) objTemp;

// Produces null.
DynamicLoadMDI. DynamicFormClas s objFormToLoad = objTemp as
DynamicLoadMDI. DynamicFormClas s;

MessageBox.Show (objFormToLoad. Location.ToStri ng());
}
// DynamicFormClas s that's defined in both MDI and DLL.
-------------------------------------------------------
namespace DynamicLoadMDI
{
public class DynamicFormClas s
{
private string _strLocation;
private string _strDescription ;

public DynamicFormClas s(string strLocation, string strDescription)
{
this._strLocati on = strLocation;
this._strDescri ption = strDescription;
}

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

public string Description
{
get {return _strDescription ;}
set {_strDescriptio n = value;}
}
}
}
Dec 12 '05 #1
1 1538
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.co m

"John F" <jf@rt.com> wrote in message
news:4C******** *************** ***********@mic rosoft.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 colAvailableFor ms;

private void btnLoad_Click(o bject sender, System.EventArg s e)
{
DynamicFormClas s objFormToLoad = colAvailableFor ms[0] as
DynamicFormClas s;

Assembly assem = Assembly.LoadFr om(objFormToLoa d.Location);

Type[] types = assem.GetTypes( );

Object genericInstance = assem.CreateIns tance(types[0].FullName);

Form tmpForm = genericInstance as Form;
tmpForm.MdiPare nt=this;
tmpForm.Show();

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

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

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

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

parameters[0] = colAvailableFor ms[0];

try
{
Object returnVal = passParam.Invok e(null,paramete rs);
}
catch(Exception ex)
{
MessageBox.Show (ex.ToString()) ;
}
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
this.colAvailab leForms = new ArrayList();
Object objNewForm = new
DynamicFormClas s("c:\\testform .dll","Descript ion");
colAvailableFor ms.Add(objNewFo rm);
}
// Child form. Code inside of testform.dll
------------------------------------------
static public void passParams(Obje ct objTemp)
{
//I've tried this and get an exception.
//DynamicLoadMDI. DynamicFormClas s objFormToLoad =
(DynamicLoadMDI .DynamicFormCla ss) objTemp;

// Produces null.
DynamicLoadMDI. DynamicFormClas s objFormToLoad = objTemp as
DynamicLoadMDI. DynamicFormClas s;

MessageBox.Show (objFormToLoad. Location.ToStri ng());
}
// DynamicFormClas s that's defined in both MDI and DLL.
-------------------------------------------------------
namespace DynamicLoadMDI
{
public class DynamicFormClas s
{
private string _strLocation;
private string _strDescription ;

public DynamicFormClas s(string strLocation, string strDescription)
{
this._strLocati on = strLocation;
this._strDescri ption = strDescription;
}

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

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

Dec 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2842
by: lawrence | last post by:
The following class method is being rejected by the PHP parser. If I change the method paramaters and allow the objects to be passed as copies, then the parser has no problem. Or, if I pass by reference but set no default value, then the parser has no problems. I've resovled this for now by taking out the "false" default values. But how shall I handle those situations (there are many) where I wish to pass an object by reference but I'm not...
38
4613
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
4
3400
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
0
1859
by: Daimy | last post by:
I meet the same problem below, please help me! Thanks! //written by some one I have developed a windows forms user control, which I am going to host in Internet Explorer.. I am familiar with the security settings requirement inorder to do the
1
1813
by: i_dvlp | last post by:
I'd like to pass a dynamic parameter or argument to a frameset, and use it as a param for the src also. I could use php to do this, but can't think of another way. I know there are JS methods getArgs() that can be used with GET requests. Maybe (for Left Hand Col) I could use something like and this would take precedence over whatever was hard-coded in the frameset page (php method is OK too, I'm just curious) any ideas ?
4
150953
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: session_set_save_handler(array(&$sess_handler, 'open'), array(&$sess_handler, 'close'), array(&$sess_handler, 'read'), array(&$sess_handler, 'write'), array(&$sess_handler, 'destroy'), array(&$sess_handler, 'gc'));
1
2547
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by reference: //Managed c++ QueryResult* qr = NULL; ExecuteQuery(1, "abc", qr); //invoke native c++ method passing qr by reference
31
6150
by: Zytan | last post by:
Everything (er, every class) in C# has ToString() which is conveniently automatically invoked when using it in Debug.WriteLine() or in a string concatenation, etc. I made a struct, and I want to make a method to print out its data in a similar format. So, I did this: public struct MyStruct { public override string ToString() {
0
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.