473,413 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 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 2177
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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...
10
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...
0
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...
0
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...
2
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...
9
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...
17
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...
0
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.