473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.A dd(objUserContr ol);

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.GetSetMe thod();
mi = piProp.Property Type.GetMethod( "Add");

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

The end result should be the same as:

Form.Controls.A dd(objUserContr ol);
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 2190
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.GetSetMe thod();
mi = piProp.Property Type.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.A dd(objUserContr ol);
--

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.goo glegroups.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.A dd(objUserContr ol);

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.GetSetMe thod();
mi = piProp.Property Type.GetMethod( "Add");

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

The end result should be the same as:

Form.Controls.A dd(objUserContr ol);
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.GetSetMe thod();
mi = piProp.Property Type.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.A dd(objUserContr ol);


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...
TargetInvocatio nException 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.GetT ype();
PropertyInfo controlCollecti onProperty = formType.GetPro perty("Controls ");
object controlCollecti on = controlCollecti onProperty.GetV alue(formObject ,
null);
Method addMethod = controlCollecti onProperty.Prop ertyType.GetMet hod("Add");
// If there are overloads, the method signature should be informed here...
addMethod.Invok e(controlCollec tion, 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.goo glegroups.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.A dd(objUserContr ol);

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.GetSetMe thod();
mi = piProp.Property Type.GetMethod( "Add");

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

The end result should be the same as:

Form.Controls.A dd(objUserContr ol);
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...
TargetInvocatio nException 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.GetT ype();
PropertyInfo controlCollecti onProperty = formType.GetPro perty("Controls ");
object controlCollecti on = controlCollecti onProperty.GetV alue(formObject ,
null);
Method addMethod = controlCollecti onProperty.Prop ertyType.GetMet hod("Add");
// If there are overloads, the method signature should be informed here...
addMethod.Invok e(controlCollec tion, 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.goo glegroups.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.A dd(objUserContr ol);

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.GetSetMe thod();
mi = piProp.Property Type.GetMethod( "Add");

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

The end result should be the same as:

Form.Controls.A dd(objUserContr ol);
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.goo glegroups.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.Control s.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.Control s = ...
this will call the set accessor. First of all Form.Controls (actually
Control.Control s) 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1369
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document what Reflection is (I can answer this question myself), what kind of Reflection Techniques there are (.NET Reflection, Python Reflection etc. I can...
10
7350
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the...
0
377
by: TT (Tom Tempelaere) | last post by:
Hi there The following is a description to reproduce the problem. I do not know if this is a bug in the .NET environment/framework (I suspect it is), or if there is something that I am doing wrong. But I am definitely stuck on this one. I cannot find a solution Make a new C# service project, and add an installer for the service (using menu)....
0
1541
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to execute button2's click event. This works great when i know the name of the method that i want to invoke. I do so by doing this: Dim...
2
1734
by: Peted | last post by:
I have 2 samples of code bellow that work fine at the moment the first routine loads an dll assembley and instantiates the type in the dll which is always a form, and creates a child form from it. I need to use latebinfing becasue the dll to be loaded is selected by the user but is allways a dll with a form class and always instantiated...
9
3842
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of an object can be constructed through reflection and method calls can be made via the reflection apis.
17
2297
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection to build a compiler? One that will construct a user defined class "on the fly" (literally, the user defines a class, instantiates it, and runs it...
0
1689
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.