How to call an object method named in a string variable ???

  • Thread starter Thread starter BBFrost
  • Start date Start date
B

BBFrost

I want to build a generic application that runs one of a set of class
methods depending on data extracted from a database table.

For example if I extract value X from the database table I also extract a
string variable that contains the corresponding method name.

string sMethodName = null;
Class1 oClass1 = new Class1( );

If Class1 has a method named Method1 and the variable sMethodName =
"Method1"

How do I construct a call to oClass1.Method1( ) using the >> contents << of
the MethodName variable ???

Thanks in advance.

Barry
in Oregon
 
Hi BBFrost

You can look at Using Activator Class. as well as Type class (see msdn doc)

This will allow you to dynamically create, manage classes and methods.

Henk
 
Hi BBFrost,

You can use Reflection to do that

MethodInfo mi = oClass1.GetType().GetMethod(sMethodName);
if(mi != null) mi.Invoke(oClass, new object[]{<method parameters go here>});

Depending on the method visibility you may need to call an overload of
GetMethod that takes BindingFlags parameter.
 
Great!

Sounds like that will work. The methods I'll be calling with be exposed as
'public' so there shouldn't be any problem. Thanks for the quick reply!
Its very much appreciated.

I'll give it a try and post back with the results.

Thanks again.

Barry
in Oregon

Stoitcho Goutsev (100) said:
Hi BBFrost,

You can use Reflection to do that

MethodInfo mi = oClass1.GetType().GetMethod(sMethodName);
if(mi != null) mi.Invoke(oClass, new object[]{<method parameters go here>});

Depending on the method visibility you may need to call an overload of
GetMethod that takes BindingFlags parameter.

--
HTH
B\rgds
100

BBFrost said:
I want to build a generic application that runs one of a set of class
methods depending on data extracted from a database table.

For example if I extract value X from the database table I also extract a
string variable that contains the corresponding method name.

string sMethodName = null;
Class1 oClass1 = new Class1( );

If Class1 has a method named Method1 and the variable sMethodName =
"Method1"

How do I construct a call to oClass1.Method1( ) using the >> contents << of
the MethodName variable ???

Thanks in advance.

Barry
in Oregon
 
I have it working. Many Thanks!

Barry
in Oregon

BBFrost said:
Great!

Sounds like that will work. The methods I'll be calling with be exposed as
'public' so there shouldn't be any problem. Thanks for the quick reply!
Its very much appreciated.

I'll give it a try and post back with the results.

Thanks again.

Barry
in Oregon

Stoitcho Goutsev (100) said:
Hi BBFrost,

You can use Reflection to do that

MethodInfo mi = oClass1.GetType().GetMethod(sMethodName);
if(mi != null) mi.Invoke(oClass, new object[]{<method parameters go here>});

Depending on the method visibility you may need to call an overload of
GetMethod that takes BindingFlags parameter.

--
HTH
B\rgds
100

BBFrost said:
I want to build a generic application that runs one of a set of class
methods depending on data extracted from a database table.

For example if I extract value X from the database table I also
extract
 
Back
Top