How do I execute a shared method using reflection?

  • Thread starter Thread starter Harry Keck
  • Start date Start date
H

Harry Keck

I really want to execute a shared method from a class
using reflection without creating an instance of the
class. I know how to use invokemethod, but it requires an
object as a parameter to invoke the method on. How can I
invoke the method without an object? I imagine that this
should be possible since you can execute a shared function
without an instance of an object if you are not using
reflection. Thanks.
 
Harry Keck said:
I really want to execute a shared method from a class
using reflection without creating an instance of the
class. I know how to use invokemethod, but it requires an
object as a parameter to invoke the method on. How can I
invoke the method without an object? I imagine that this
should be possible since you can execute a shared function
without an instance of an object if you are not using
reflection. Thanks.

You just pass null as the parameter, as per the docs:

<quote>
To invoke a static method using its MethodInfo object, the first
parameter should be a null reference (Nothing), as shown in the
following call:

Object myReturnValue = myMethodInfo.Invoke(null, myParametersArray);
</quote>

(It should really say that in the parameter description though, IMO.)
 
Jon : a small doubt
i guess you can pass null. But even if that works it will
work only if the static variable is in the current class
only. If it is in a different class then how do i call
GetMethod. That needs a type object. Even if i somehow
get the methodInfo object, passing null will not allow me
to invoke that method with null coz null will not link to
that class in any way.

But using the GetType definitely works. Ill give you an
example that i posted for a similar post in one of our
usergorups. In that it was accessing a member variable.
Her in your case it is a member function. As such there
is no difference. Only thing is that use
MethodInfo instead of FieldInfo
GetMethod instead of GetField
and Invoke instead of SetValue
in the sample below

<previous_post>
Remember that static variables/functions are those
variables/functions which are per-type. this means that
while normal member variables/fucntions belong to the
object instance, the shared/static variables/functions
belong to type.

So this means that if you pass the object as parameter to
GetValue for instance variables, from the previous
paragraph,you can pass the type of the class for
static/shared variables. here is a sample


-----------------------------------------------------------
Watch Out for Line wraps.....
---------------------

Imports System.Reflection
Module Module1
Sub Main()
fnSetStaticValueDirectly()
Console.WriteLine(Class1.i) ' will print 10
fnSetStaticValueThroughReflection()
Console.WriteLine(Class1.i) ' will print 20
Console.ReadLine()
End Sub

Public Sub fnSetStaticValueDirectly()
Class1.i = 10
End Sub

Public Sub fnSetStaticValueThroughReflection()
Dim l_objType As Type
Dim l_objFieldInfo As FieldInfo
l_objFieldInfo = GetType(Class1).GetField("i",
BindingFlags.Static Or BindingFlags.GetField Or
BindingFlags.NonPublic Or BindingFlags.Public)
l_objFieldInfo.SetValue(GetType(Class1), 20)
End Sub

End Module

Class Class1
Public Shared i As Integer
End Class

</previous_post>
-----------------------------------------------------------
---------------------


hth. In case of problems do get back

regards,

sr
 
Jon : a small doubt
i guess you can pass null. But even if that works it will
work only if the static variable is in the current class
only. If it is in a different class then how do i call
GetMethod. That needs a type object.

Yes, but you don't need to have an instance to get a type - you can
just use GetType...
Even if i somehow
get the methodInfo object, passing null will not allow me
to invoke that method with null coz null will not link to
that class in any way.

Yes it will, because the MethodInfo knows what type it relates to.

I think it's easiest to show you with an example which will work. It's
in C#, but I'm sure you'll get the drift...

using System;
using System.Reflection;

public class NeverInstantiated
{
public static void Foo(string x)
{
Console.WriteLine ("Hello "+x);
}
}

public class Test
{
static void Main()
{
// Get the type
Type type = Type.GetType ("NeverInstantiated");

// Get the relevant method
MethodInfo fooMethod = type.GetMethod
("Foo", new Type[]{typeof(string)});

// Invoke it with a parameter
fooMethod.Invoke (null, new object[]{"Jon"});
}
}
 
Back
Top