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).
dvplayltdAsked:
Who is Participating?
 
Refael AckermannConnect With a Mentor Commented:
I get it now...
I believe the best way to solve this is to create a common base type (public class MyForm : Form {public Form mdtForm ...}...).
But anyway, here's how I'd reflect it:

private void buttonGrdView_Click(object sender, EventArgs e)
{
	object activeForm = this.ActiveMdiChild;
	try
	{
		object mdtForm = activeForm.GetType().GetField("mdtForm").GetValue(activeForm);
		MethodInfo changeView = mdtForm.GetType().GetMethod("changeView");
		changeView.Invoke(mdtForm, new object[] {true});
	}

Open in new window

0
 
Refael AckermannCommented:
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

0
 
Refael AckermannCommented:
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

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
dvplayltdAuthor Commented:
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 ?
0
 
Refael AckermannCommented:
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

0
 
dvplayltdAuthor Commented:
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.
0
 
Refael AckermannCommented:
What would you want to do if the active from is not of type frmCampaign?
0
 
dvplayltdAuthor Commented:
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.
0
 
dvplayltdAuthor Commented:
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 !
0
 
Refael AckermannCommented:
Happy to help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.