?
Solved

How to use Reflection to call members object

Posted on 2008-11-13
10
Medium Priority
?
384 Views
Last Modified: 2013-12-17
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).
0
Comment
Question by:dvplayltd
  • 6
  • 4
10 Comments
 
LVL 7

Expert Comment

by:moseack
ID: 22954424
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
 
LVL 7

Expert Comment

by:moseack
ID: 22954494
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
 

Author Comment

by:dvplayltd
ID: 22955337
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 7

Expert Comment

by:moseack
ID: 22955819
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
 

Author Comment

by:dvplayltd
ID: 22958475
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
 
LVL 7

Expert Comment

by:moseack
ID: 22958498
What would you want to do if the active from is not of type frmCampaign?
0
 

Author Comment

by:dvplayltd
ID: 22958597
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
 
LVL 7

Accepted Solution

by:
moseack earned 2000 total points
ID: 22958733
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
 

Author Closing Comment

by:dvplayltd
ID: 31516540
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
 
LVL 7

Expert Comment

by:moseack
ID: 22958830
Happy to help
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question