views:

50

answers:

2

the application is very large so giving a brief back ground and the problem

when the user logs in, a button is displayed having the text of the function he is allowed to call.

the function he is allowed is mapped in the database table

its made sure that the name of the actual function is same to the ones used in the db.

problem

the name is extracted, and stored as text field of button and also in a string variable.

now how am i supposed to call this function using the string variable which has the name stored in it!

like we type

name-of-function();

but here i dont know the name, the string at run time does so i cant write like

string()!!?

+2  A: 

You will need to use reflection to do this. Here is a rough sketch of what you need to do:

// Get the Type on which your method resides:
Type t = typeof(SomeType);
// Get the method
MethodInfo m = t.GetMethod("methodNameFromDb");
// Invoke dynamically
m.Invoke(instance, null);

Depending on your actual needs you will have to modify this a little - lookup the used methods and types on MSDN: MethodInfo, Invoke

driis
While this is the technical answer to the question, I would not recommend going down this path. See the other answer: http://stackoverflow.com/questions/3918431/how-to-achieve-calling-a-function-dynamically-thats-right-the-function-to-be-cal/3918497#3918497
driis
i think u must mean the other link.... one confusion, what is the use of type?? i have all the functions in the same class, so can the above or what mentioned in othe answer be achieved without using the type??
please check http://stackoverflow.com/questions/3975659/calling-the-function-which-is-stored-in-a-string-variable
+2  A: 

Well, no matter what you do, there is going to have to be some kind of mapping done between a database "function" and your "real" function. You can probably use Reflection using your Types and MethodInfo.

However, this sounds like a maintenance nightmare. It also sounds like you are reinventing user roles or the like. I would be very cautious about going down this path - I think it will be much more complex and problematic than you think.

Wonko the Sane
thats exactly what i have to do..
please check http://stackoverflow.com/questions/3975659/calling-the-function-which-is-stored-in-a-string-variable