Calling a static method by name

  • Thread starter Thread starter Gilgamesh
  • Start date Start date
G

Gilgamesh

Hi there,
Is there a way to call a static method dynamically by knowing the name of
the method? In my case, I get the name of the method form a table, and I
would need to call a static method of a class which has the same name. Is
there anyway to do this?
Thanks,
Gilgamesh
 
Greets,

This is a perfect candidate for reflection. Assuming you have the
following function signature:

public static bool Initialize()

You can use the following code (add using System.Reflection;) to create
an instance of the type for your class (change "Namespace" and "Classname"
as appropriate) and then invoke the member function. Note that I used the
'Public', 'InvokeMethod' and 'Static' binding flags appropriate for the
call. The return value from the call is an object, so cast to the
appropriate return value from your method call. This example passes an
empty object[] array since I didn't take any parameters, but if you take
parameters, just package up your individual parameters in the object array.

Regards,

Joe
 
Of course, it would help if I included the code. <oops>

Type theClass = assembly.GetType("Namespace.Classname", false, true);

bool result = (bool)theClass.InvokeMember("Initialize", BindingFlags.Public
| BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new object[]
{ });

Joe Delekto said:
Greets,

This is a perfect candidate for reflection. Assuming you have the
following function signature:

public static bool Initialize()

You can use the following code (add using System.Reflection;) to create
an instance of the type for your class (change "Namespace" and "Classname"
as appropriate) and then invoke the member function. Note that I used the
'Public', 'InvokeMethod' and 'Static' binding flags appropriate for the
call. The return value from the call is an object, so cast to the
appropriate return value from your method call. This example passes an
empty object[] array since I didn't take any parameters, but if you take
parameters, just package up your individual parameters in the object array.

Regards,

Joe

Gilgamesh said:
Hi there,
Is there a way to call a static method dynamically by knowing the name of
the method? In my case, I get the name of the method form a table, and I
would need to call a static method of a class which has the same name. Is
there anyway to do this?
Thanks,
Gilgamesh
 
Hi Gilgamesh,

You can do that via Reflection

typeof(<type name>).GetMethod("<method name>").Invoke(null, new
object[]{<lsit of parameters if any>});

In GetMethod you might need to specify some BindingFlags if the method is
not public
 
Hi Joe,

The class which contains the static methods is not intantiable. Here is more
details:
Main Class:
Class Name: public sealed class d3
Constructor: private d3() {}
Static Method within d3 class: public static DataSet GetInventory

I read the method name (i.e. "GetInventory") from a database and I would
need to call d3.GetInventory.

Thanks for your help,
G.



Joe Delekto said:
Of course, it would help if I included the code. <oops>

Type theClass = assembly.GetType("Namespace.Classname", false, true);

bool result = (bool)theClass.InvokeMember("Initialize", BindingFlags.Public
| BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new object[]
{ });

Joe Delekto said:
Greets,

This is a perfect candidate for reflection. Assuming you have the
following function signature:

public static bool Initialize()

You can use the following code (add using System.Reflection;) to create
an instance of the type for your class (change "Namespace" and "Classname"
as appropriate) and then invoke the member function. Note that I used the
'Public', 'InvokeMethod' and 'Static' binding flags appropriate for the
call. The return value from the call is an object, so cast to the
appropriate return value from your method call. This example passes an
empty object[] array since I didn't take any parameters, but if you take
parameters, just package up your individual parameters in the object array.

Regards,

Joe

Gilgamesh said:
Hi there,
Is there a way to call a static method dynamically by knowing the name of
the method? In my case, I get the name of the method form a table, and I
would need to call a static method of a class which has the same name. Is
there anyway to do this?
Thanks,
Gilgamesh
 
Greets,

The code I showed doesn't require an instance of your object to be
created. Just make sure that the assembly referenced in that code is where
the object is located. (If in your current executing assembly, just use
Assembly.GetExecutingAssembly().) Also, I noted that the GetType() call I
used ignores cases, you may wish to make the 'true' actually false if you
want case sensitivity.

I tried the code with a class using the same class specification you
provided and had no problem getting it to work.

Regards,

Joe

Gilgamesh said:
Hi Joe,

The class which contains the static methods is not intantiable. Here is more
details:
Main Class:
Class Name: public sealed class d3
Constructor: private d3() {}
Static Method within d3 class: public static DataSet GetInventory

I read the method name (i.e. "GetInventory") from a database and I would
need to call d3.GetInventory.

Thanks for your help,
G.



Joe Delekto said:
Of course, it would help if I included the code. <oops>

Type theClass = assembly.GetType("Namespace.Classname", false, true);

bool result = (bool)theClass.InvokeMember("Initialize", BindingFlags.Public
| BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new object[]
{ });

Joe Delekto said:
Greets,

This is a perfect candidate for reflection. Assuming you have the
following function signature:

public static bool Initialize()

You can use the following code (add using System.Reflection;) to create
an instance of the type for your class (change "Namespace" and "Classname"
as appropriate) and then invoke the member function. Note that I used the
'Public', 'InvokeMethod' and 'Static' binding flags appropriate for the
call. The return value from the call is an object, so cast to the
appropriate return value from your method call. This example passes an
empty object[] array since I didn't take any parameters, but if you take
parameters, just package up your individual parameters in the object array.

Regards,

Joe

Hi there,
Is there a way to call a static method dynamically by knowing the
name
of
the method? In my case, I get the name of the method form a table,
and
I name.
Is
 
Hi Stoitcho,
It seems like using this technique I would be able to get the information
for the static method. Would I be able to invoke the method too? And is so,
can you provide and example?

Thanks,
G.


Stoitcho Goutsev (100) said:
Hi Gilgamesh,

You can do that via Reflection

typeof(<type name>).GetMethod("<method name>").Invoke(null, new
object[]{<lsit of parameters if any>});

In GetMethod you might need to specify some BindingFlags if the method is
not public

--
HTH
B\rgds
100 [C# MVP]

Gilgamesh said:
Hi there,
Is there a way to call a static method dynamically by knowing the name of
the method? In my case, I get the name of the method form a table, and I
would need to call a static method of a class which has the same name. Is
there anyway to do this?
Thanks,
Gilgamesh
 
Joe,
I used your logic to access the method. However, I'm getting a run time
error when "InvokeMember" is called. The error I'm getting is this: "Object
reference not set to an instance of an object". If I put a break point on
the call, step through the code, I could see that the error occurs before
the static method is actually called. Any help would be appreciated.

Thanks,
G.


Joe Delekto said:
Greets,

The code I showed doesn't require an instance of your object to be
created. Just make sure that the assembly referenced in that code is where
the object is located. (If in your current executing assembly, just use
Assembly.GetExecutingAssembly().) Also, I noted that the GetType() call I
used ignores cases, you may wish to make the 'true' actually false if you
want case sensitivity.

I tried the code with a class using the same class specification you
provided and had no problem getting it to work.

Regards,

Joe

Gilgamesh said:
Hi Joe,

The class which contains the static methods is not intantiable. Here is more
details:
Main Class:
Class Name: public sealed class d3
Constructor: private d3() {}
Static Method within d3 class: public static DataSet GetInventory

I read the method name (i.e. "GetInventory") from a database and I would
need to call d3.GetInventory.

Thanks for your help,
G.



Joe Delekto said:
Of course, it would help if I included the code. <oops>

Type theClass = assembly.GetType("Namespace.Classname", false, true);

bool result = (bool)theClass.InvokeMember("Initialize", BindingFlags.Public
| BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new object[]
{ });

Greets,

This is a perfect candidate for reflection. Assuming you have the
following function signature:

public static bool Initialize()

You can use the following code (add using System.Reflection;) to
create
an instance of the type for your class (change "Namespace" and "Classname"
as appropriate) and then invoke the member function. Note that I
used
the
'Public', 'InvokeMethod' and 'Static' binding flags appropriate for the
call. The return value from the call is an object, so cast to the
appropriate return value from your method call. This example passes an
empty object[] array since I didn't take any parameters, but if you take
parameters, just package up your individual parameters in the object
array.

Regards,

Joe

Hi there,
Is there a way to call a static method dynamically by knowing the name
of
the method? In my case, I get the name of the method form a table,
and
I
would need to call a static method of a class which has the same name.
Is
there anyway to do this?
Thanks,
Gilgamesh
 
Okay, I fixed the error. Now, how do you pass an array to the static method
which is invoked by "InvokeMember"? In my case, the static method is
expecting an array of parameters to be passed in, but it seems like
InvokeMember only allows passing argument lists.

Thanks,
G.



Gilgamesh said:
Joe,
I used your logic to access the method. However, I'm getting a run time
error when "InvokeMember" is called. The error I'm getting is this: "Object
reference not set to an instance of an object". If I put a break point on
the call, step through the code, I could see that the error occurs before
the static method is actually called. Any help would be appreciated.

Thanks,
G.


Joe Delekto said:
Greets,

The code I showed doesn't require an instance of your object to be
created. Just make sure that the assembly referenced in that code is where
the object is located. (If in your current executing assembly, just use
Assembly.GetExecutingAssembly().) Also, I noted that the GetType() call I
used ignores cases, you may wish to make the 'true' actually false if you
want case sensitivity.

I tried the code with a class using the same class specification you
provided and had no problem getting it to work.

Regards,

Joe

Gilgamesh said:
Hi Joe,

The class which contains the static methods is not intantiable. Here
is
more
details:
Main Class:
Class Name: public sealed class d3
Constructor: private d3() {}
Static Method within d3 class: public static DataSet GetInventory

I read the method name (i.e. "GetInventory") from a database and I would
need to call d3.GetInventory.

Thanks for your help,
G.



Of course, it would help if I included the code. <oops>

Type theClass = assembly.GetType("Namespace.Classname", false, true);

bool result = (bool)theClass.InvokeMember("Initialize",
BindingFlags.Public
| BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new
object[]
{ });

Greets,

This is a perfect candidate for reflection. Assuming you have the
following function signature:

public static bool Initialize()

You can use the following code (add using System.Reflection;) to
create
an instance of the type for your class (change "Namespace" and
"Classname"
as appropriate) and then invoke the member function. Note that I used
the
'Public', 'InvokeMethod' and 'Static' binding flags appropriate
for
the
call. The return value from the call is an object, so cast to the
appropriate return value from your method call. This example
passes
an
empty object[] array since I didn't take any parameters, but if
you
take
parameters, just package up your individual parameters in the object
array.

Regards,

Joe

Hi there,
Is there a way to call a static method dynamically by knowing
the
name
of
the method? In my case, I get the name of the method form a
table,
and
I
would need to call a static method of a class which has the same name.
Is
there anyway to do this?
Thanks,
Gilgamesh
 
Back
Top