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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.