471,337 Members | 1,193 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Stuck with reflection C#

Hello All:

My second post in as many days! I'm trying to get this working.

I have a form and a user control and trying to add the user control to
this form. Without reflection this would be achieved as

Form.Controls.Add(objUserControl);

Now in my code I have the following:

objUserControl = User Control Object obtained by reflection.
mi is the MethodInfo for the Add method obtained as:

MethodInfo mi = piProp.GetSetMethod();
mi = piProp.PropertyType.GetMethod("Add");

objUserControl.GetType().BaseType returns System.Windows.Forms.Control.
I want to get this working:
mi.Invoke(objForm, new object[] {objUserControl });
But it wouldnt let me as objUserControl causes a
TargetInvocationException. Please let me know how I can handle this.

The end result should be the same as:

Form.Controls.Add(objUserControl);
I know this should be something quite simple, but I'm clearly missing
something and am new to reflection.

Thanks in Advance.
Skanda

Feb 20 '06 #1
5 2119
Skandy,

You obtain the user control via reflection, but Why do you need reflection
to set this control to the Forms.Comtrols collection?
MethodInfo mi = piProp.GetSetMethod();
mi = piProp.PropertyType.GetMethod("Add");
This code looks little bit fishy to me.

Do you have reference to the from?

What is the piProp. It is PorpertyInfo for what property?

Why do you get the *set* accessor if you need somethinf it is the *get*
accessor

You get the mi for the *set* accessor and right after you use the same
variable to get the MethodInfo for the Add. I don't understand that.

Keep in mind that Controls is not a static property you need to have a
reference to the form. Having reference to the form I don't see why you need
reflection to add the control.
Form.Controls.Add(objUserControl);
--

Stoitcho Goutsev (100)
This makes me think that you treat the Controls collection as a static one.

"Skandy" <sk****************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... Hello All:

My second post in as many days! I'm trying to get this working.

I have a form and a user control and trying to add the user control to
this form. Without reflection this would be achieved as

Form.Controls.Add(objUserControl);

Now in my code I have the following:

objUserControl = User Control Object obtained by reflection.
mi is the MethodInfo for the Add method obtained as:

MethodInfo mi = piProp.GetSetMethod();
mi = piProp.PropertyType.GetMethod("Add");

objUserControl.GetType().BaseType returns System.Windows.Forms.Control.
I want to get this working:
mi.Invoke(objForm, new object[] {objUserControl });
But it wouldnt let me as objUserControl causes a
TargetInvocationException. Please let me know how I can handle this.

The end result should be the same as:

Form.Controls.Add(objUserControl);
I know this should be something quite simple, but I'm clearly missing
something and am new to reflection.

Thanks in Advance.
Skanda

Feb 20 '06 #2
Hi Stoitcho:

Thanks for your reply.

You obtain the user control via reflection, but Why do you need
reflection
to set this control to the Forms.Comtrols collection?
MethodInfo mi = piProp.GetSetMethod();
mi = piProp.PropertyType.GetMethod("Add");

This code looks little bit fishy to me.

Do you have reference to the from?
What is the piProp. It is PorpertyInfo for what property? The piProp is
the reference to PropertyInfo piProp =
objForm.GetType().GetProperty("Controls");
My bad! I dint mention it earlier.

You get the mi for the *set* accessor and right after you use the same
variable to get the MethodInfo for the Add. I don't understand that.

I get the "Add" method for the the GetSet accessor
Keep in mind that Controls is not a static property you need to have a
reference to the form. Having reference to the form I don't see why you
need
reflection to add the control.
Form.Controls.Add(objUserControl);


The reason why I'm using reflection is because I dont want use a
reference to the System.Windows.Forms in my console application and
hence use reflection to add the usercontrol to the Form object,
objForm.

Thanks.
Skanda

Feb 20 '06 #3
Only to help you on this problem...
TargetInvocationException means that the method called using Reflection
threw an exception...
You should check the InnerException of the exception raised to have more
details about what happened when you called the method throught
reflection... This is the first step, I guess...
Discover what is the type of "Form.Controls". Is it some specialized
collection?
I would use the following code:

Type formType = formObject.GetType();
PropertyInfo controlCollectionProperty = formType.GetProperty("Controls");
object controlCollection = controlCollectionProperty.GetValue(formObject,
null);
Method addMethod = controlCollectionProperty.PropertyType.GetMethod(" Add");
// If there are overloads, the method signature should be informed here...
addMethod.Invoke(controlCollection, new object[] { your control here }); //
This should work...

But take a look at InnerException property of the exception raised to have
more details about the error.

"Skandy" <sk****************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hello All:

My second post in as many days! I'm trying to get this working.

I have a form and a user control and trying to add the user control to
this form. Without reflection this would be achieved as

Form.Controls.Add(objUserControl);

Now in my code I have the following:

objUserControl = User Control Object obtained by reflection.
mi is the MethodInfo for the Add method obtained as:

MethodInfo mi = piProp.GetSetMethod();
mi = piProp.PropertyType.GetMethod("Add");

objUserControl.GetType().BaseType returns System.Windows.Forms.Control.
I want to get this working:
mi.Invoke(objForm, new object[] {objUserControl });
But it wouldnt let me as objUserControl causes a
TargetInvocationException. Please let me know how I can handle this.

The end result should be the same as:

Form.Controls.Add(objUserControl);
I know this should be something quite simple, but I'm clearly missing
something and am new to reflection.

Thanks in Advance.
Skanda

Feb 20 '06 #4
Hi Ravi:

Thanks a lot.
That code helped me.


Ravi Ambros Wallau wrote:
Only to help you on this problem...
TargetInvocationException means that the method called using Reflection
threw an exception...
You should check the InnerException of the exception raised to have more
details about what happened when you called the method throught
reflection... This is the first step, I guess...
Discover what is the type of "Form.Controls". Is it some specialized
collection?
I would use the following code:

Type formType = formObject.GetType();
PropertyInfo controlCollectionProperty = formType.GetProperty("Controls");
object controlCollection = controlCollectionProperty.GetValue(formObject,
null);
Method addMethod = controlCollectionProperty.PropertyType.GetMethod(" Add");
// If there are overloads, the method signature should be informed here...
addMethod.Invoke(controlCollection, new object[] { your control here }); //
This should work...

But take a look at InnerException property of the exception raised to have
more details about the error.

"Skandy" <sk****************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hello All:

My second post in as many days! I'm trying to get this working.

I have a form and a user control and trying to add the user control to
this form. Without reflection this would be achieved as

Form.Controls.Add(objUserControl);

Now in my code I have the following:

objUserControl = User Control Object obtained by reflection.
mi is the MethodInfo for the Add method obtained as:

MethodInfo mi = piProp.GetSetMethod();
mi = piProp.PropertyType.GetMethod("Add");

objUserControl.GetType().BaseType returns System.Windows.Forms.Control.
I want to get this working:
mi.Invoke(objForm, new object[] {objUserControl });
But it wouldnt let me as objUserControl causes a
TargetInvocationException. Please let me know how I can handle this.

The end result should be the same as:

Form.Controls.Add(objUserControl);
I know this should be something quite simple, but I'm clearly missing
something and am new to reflection.

Thanks in Advance.
Skanda


Feb 20 '06 #5
Skandy
"Skandy" <sk****************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
What is the piProp. It is PorpertyInfo for what property? The piProp is
the reference to PropertyInfo piProp =
objForm.GetType().GetProperty("Controls");
My bad! I dint mention it earlier.

See you have reference to the form - objForm. You use it to get the
PropertyInfo for the Controls collection. I believe you can just use it to
set the control.

objForm.Controls.Add(...)
You get the mi for the *set* accessor and right after you use the same
variable to get the MethodInfo for the Add. I don't understand that.

I get the "Add" method for the the GetSet accessor
Here is what is wrong. Set accesso is a method to set the Controls property.
when you call
objForm.Controls = ...
this will call the set accessor. First of all Form.Controls (actually
Control.Controls) is a read only it doesn have a *set* accessor. You will
get nothing wiht this refelection.

What you need actually is to call piPorp.GetValue(...) to get reference to
the Controls collection and the reflect on this reference to get the Add
method.

The reason why I'm using reflection is because I dont want use a
reference to the System.Windows.Forms in my console application and
hence use reflection to add the usercontrol to the Form object,
objForm.


It is your call, but frankly I don't see a problem referencing
System.Windows.Forms.dll. You trade performance, ease of maintenance and
readability for referencing one DLL, but you probably have your reasons for
that.
--
HTH
Stoitcho Goutsev (100)
Feb 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by A. Wiebenga | last post: by
reply views Thread by TT (Tom Tempelaere) | last post: by
9 posts views Thread by Kuberan Naganathan | last post: by
17 posts views Thread by raylopez99 | last post: by
reply views Thread by Gustavo Arriola | last post: by
reply views Thread by rosydwin | last post: by

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.