473,326 Members | 2,175 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,326 software developers and data experts.

Non-static method requires a target

TJO
I am trying to load an assembly, instantiate a class and invoke a
non-static void method using the following code:

I am getting the error "Non-static method requires a target" on the
InvokeMember method but don't see why. Can anyone shed light on this??


Assembly testAssembly =
Assembly.LoadFile(@"C:\STProjects\..\MyAssembly.dl l");

Type testType =
testAssembly.GetType(this._cmbo_TestClasses.Text.T rim());

object[] args = new object[]{"myparam"};

SmartMI_TestClasses.HDDUtilization HDDtest =
(SmartMI_TestClasses.HDDUtilization)
testAssembly.CreateInstance(
testType.Name,
false, BindingFlags.Default, null,
args, null, null);
object o = testType.InvokeMember(
"_ExecuteTest",
BindingFlags.InvokeMethod,
null, HDDtest, null);

Nov 17 '05 #1
9 38224
Maybe CreateInstance returned null because it couldn't find the class
testType.Name in testAssembly?

Nov 17 '05 #2
Ron
You need to create an instance of the class first, then call the method.

"TJO" wrote:
I am trying to load an assembly, instantiate a class and invoke a
non-static void method using the following code:

I am getting the error "Non-static method requires a target" on the
InvokeMember method but don't see why. Can anyone shed light on this??


Assembly testAssembly =
Assembly.LoadFile(@"C:\STProjects\..\MyAssembly.dl l");

Type testType =
testAssembly.GetType(this._cmbo_TestClasses.Text.T rim());

object[] args = new object[]{"myparam"};

SmartMI_TestClasses.HDDUtilization HDDtest =
(SmartMI_TestClasses.HDDUtilization)
testAssembly.CreateInstance(
testType.Name,
false, BindingFlags.Default, null,
args, null, null);
object o = testType.InvokeMember(
"_ExecuteTest",
BindingFlags.InvokeMethod,
null, HDDtest, null);

Nov 17 '05 #3
TJO
Does'nt the CreateInstance statement just before the InvokeMember
statement do that?

Nov 17 '05 #4
Otherwise, try getting the MethodInfo from the Type for the method that
you want to invoke, check that it's finding it (not null), and then use
MethodInfo.Invoke

Nov 17 '05 #5
TJO
The CreateInstance method is not returning the object as I expected.
Instead it is a null reference!?? I am not sure how to use the
MethodInfo in this situation. MSDN docs are not providing much
guidance. Any code snippets or other suggestions HIGHLY welcome here.

Nov 17 '05 #6
TJO
This is some simple code that works:

object[] args = new object[]{machine[0].ToString()};

object
ob = Activator.CreateInstance(testType, args);

The only problem is when I attempt to cast ob to the proper class I get
a cast exception error.

Anyone see why?

Nov 17 '05 #7
Ron
This one should work:

//create instance assuming default constructor
object obj = testType.InvokeMember(null, BindingFlags.Public |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
//call method now
tp.InvokeMember("_ExecuteTest", BindingFlags.InvokeMethod, null, obj, null);

"TJO" wrote:
Does'nt the CreateInstance statement just before the InvokeMember
statement do that?

Nov 17 '05 #8
jim
This did work thank you.

Why does the following code not work once the obj is instantiated??

myclass c = (myclass)obj;

I am now getting invalid cast!?

"Ron" wrote:
This one should work:

//create instance assuming default constructor
object obj = testType.InvokeMember(null, BindingFlags.Public |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
//call method now
tp.InvokeMember("_ExecuteTest", BindingFlags.InvokeMethod, null, obj, null);

"TJO" wrote:
Does'nt the CreateInstance statement just before the InvokeMember
statement do that?

Nov 17 '05 #9
Ron
obj is an instance of a class for which you have no direct reference. In
VB.NET you could just use late binding and call the method directly
(obj.myMethod), but C# doesn't support late binding at the language level. As
far as I know, you can only use this instance through reflection.

"jim" wrote:
This did work thank you.

Why does the following code not work once the obj is instantiated??

myclass c = (myclass)obj;

I am now getting invalid cast!?

"Ron" wrote:
This one should work:

//create instance assuming default constructor
object obj = testType.InvokeMember(null, BindingFlags.Public |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
//call method now
tp.InvokeMember("_ExecuteTest", BindingFlags.InvokeMethod, null, obj, null);

"TJO" wrote:
Does'nt the CreateInstance statement just before the InvokeMember
statement do that?

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
17
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
2
by: Mark Stijnman | last post by:
I would like to be able to have an object accessible as a vector using the operator, but able to track modifications to its data, so that it can update other internal data as needed. I figured...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.