Link to home
Start Free TrialLog in
Avatar of dvplayltd
dvplayltdFlag for Bulgaria

asked on

How to use Reflection to call members object

Hi experts!

I created WinForm application with C# 2008 NET framework 2.0. I need example how to call method with Reflection. Look follow code.

We have form, which have public object  mdtForm . DataSpread is other class which has metods.

    public partial class frmCampaign : Form
    {

        public DataSpread mdtForm;

       public frmCampaign()
        {
            InitializeComponent();
        }

     // .. and so on..

}

This form is MDI child. I want to use Reflection to call something like:
frmCampaign.mdtForm.ViewChange() but with reflecation. I need to use Reflection, becaouse I need to call metod of activeform which may be different from frmCampaign.

Here is example which works OK how to execute procedure of form. But I need example how to execute frmCampaign.mdtForm.ChangeView.


        private void buttonGrdView_Click(object sender, EventArgs e)
{
            Form activeForm = this.ActiveMdiChild;

            try
            {
                Type m = activeForm.GetType();
                MethodInfo a = m.GetMethod("changeview");
               
                try
                {
                    object[] a1;
                    a1 = new object[1];
                    a1[0] = true;
                    a.Invoke(activeForm,a1);  //= frmCampaign.ViewChange(true) and work OK
                                                               // But I want to call frmCampaign.mdtForm.ViewChange(true).
Avatar of Refael Ackermann
Refael Ackermann
Flag of United States of America image

Something along the lines of:

private void buttonGrdView_Click(object sender, EventArgs e)
{
	try
	{
		MethodInfo a = frmCampaign.mdtForm.GetType().GetMethod("changeview");
 
		try
		{
			a.Invoke(frmCampaign.mdtForm, new object[] {true});

Open in new window

BTW: if you need it just for doing stuff on a different Thread you can use BeginInvoke

private void buttonGrdView_Click(object sender, EventArgs e)
{
   BeginInvoke(new InvokeDelegate(InvokeMethod));
}
public void InvokeMethod()
{
   frmCampaign.mdtForm.ViewChange(true);
}

Open in new window

Avatar of dvplayltd

ASKER

For moseack:
Thank you for you answer, but it is not complate. Problem is that I get frmCampaign like Form and compiler don't accept frm.mdtForm beacuse in general there no mdtForm in general form class. Or I get this form like             Form activeForm = this.ActiveMdiChild;

and also there no activeForm.mdtForm .


Form may be frmCapaign, but may be other form with other name, that is why I need to use reflaction.
Can you expand you example with this notice ?
Then try to find the type of the form like so:

frmCampaign activeFormCampaign = this.ActiveMdiChild as frmCampaign;  // Casting
if (activeFormCampaign != null)
  activeFormCampaign.mdtForm.ViewChange(true);

Open in new window

To moseack:
It is not sutable, beacuse form may be frmCampagin, but may be other more then 10 forms

I need to use reflcation and probably with GetField to get reference to mdtForm of ActiveForm and then to excute it's method changeview (of mdtForm). Give example of this.
What would you want to do if the active from is not of type frmCampaign?
I will have about 10 forms and every form will have mdtForm fileds which will have ViewChange metod. No matter which is active form, I want to get reference to it mdtForm and to excute method.

This code will be used in MDIParent form with  toolbar with button MoveNextRecord, MovePrevoius, ChangeView and so on ... This operation will be done over currently activeform in MDI child form.
ASKER CERTIFIED SOLUTION
Avatar of Refael Ackermann
Refael Ackermann
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much! This looking what I aks for! BTW: you are right, I already make class with mdtForm and after I inherit it i have changeview like method of form, not like method of mdtForm . Hoever, I will use you example in other situation. Thank a lot !
Happy to help