473,386 Members | 1,720 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,386 software developers and data experts.

Reflection Question

I'm trying to invoke a method that returns a value (actually a Panel), but i
keep getting an error:

System.Reflection.TargetException was unhandled
Message="Object does not match target type."

Here's the code:

MethodInfo methodInfo = typeChannelPanel.GetMethod("CreateNavPanel");

Panel o_Panel = (Panel)methodInfo.Invoke(methodInfo, null);

I've tried creating it as an Object and i get the same error. The
CreateNavPanel returns a Panel, which i then need to add to the main form.
What am i missing here?

Dec 1 '05 #1
2 4510
The first parameter of methodInfo.Invoke is the target object i.e. if you
were doing it in "normal" code

myPanel.CreateNavPanel();

then the equivalent in reflection would be

methodInfo.Invoke(myPanel, null);

Does that make sense? The only exception is for static methods, where you
can pass null into the first argument.

Your code attempts to execute the method on the methodInfo object, hence the
error.

Marc

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:u6**************@TK2MSFTNGP14.phx.gbl...
I'm trying to invoke a method that returns a value (actually a Panel), but
i keep getting an error:

System.Reflection.TargetException was unhandled
Message="Object does not match target type."

Here's the code:

MethodInfo methodInfo = typeChannelPanel.GetMethod("CreateNavPanel");

Panel o_Panel = (Panel)methodInfo.Invoke(methodInfo, null);

I've tried creating it as an Object and i get the same error. The
CreateNavPanel returns a Panel, which i then need to add to the main form.
What am i missing here?

Dec 1 '05 #2
Marc,

Thanks...figured it out.

Cheers.
"Marc Gravell" <mg******@rm.com> wrote in message
news:O3**************@TK2MSFTNGP15.phx.gbl...
The first parameter of methodInfo.Invoke is the target object i.e. if you
were doing it in "normal" code

myPanel.CreateNavPanel();

then the equivalent in reflection would be

methodInfo.Invoke(myPanel, null);

Does that make sense? The only exception is for static methods, where you
can pass null into the first argument.

Your code attempts to execute the method on the methodInfo object, hence
the error.

Marc

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:u6**************@TK2MSFTNGP14.phx.gbl...
I'm trying to invoke a method that returns a value (actually a Panel),
but i keep getting an error:

System.Reflection.TargetException was unhandled
Message="Object does not match target type."

Here's the code:

MethodInfo methodInfo = typeChannelPanel.GetMethod("CreateNavPanel");

Panel o_Panel = (Panel)methodInfo.Invoke(methodInfo, null);

I've tried creating it as an Object and i get the same error. The
CreateNavPanel returns a Panel, which i then need to add to the main
form. What am i missing here?


Dec 1 '05 #3

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...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
2
by: Dan | last post by:
Let's say I have a class like: class Dummy { public const string CONE = "one"; public const string CTWO = "two"; ... other stuff .... }
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
1
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
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,...
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...

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.