Stuck with reflection C#

S

Skandy

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
 
S

Stoitcho Goutsev \(100\)

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 said:
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
 
S

Skandy

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
 
R

Ravi Ambros Wallau

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.
 
S

Skandy

Hi Ravi:

Thanks a lot.
That code helped me.



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 said:
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
 
S

Stoitcho Goutsev \(100\)

Skandy
Skandy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top