Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,

Need some advice.

I got a string in the database:
"PerformInjection{param}DataDict{param}ObjectArray"

Now - I parse this string and split it into array:
C#
[0] = "PerformInection"
[1] = "DataDict"
[2] = "ObjectArray"


Now what I need to do is to run the function, which name is in [0] and pass other string array elements as parameters to it. So finaly - such code should get executed:

C#
PerformInjection(DataDict, ObjectArray);


Any thought on how to achieve this in the most graceful manner? (Please note that in this example I got only 2 parameters that should be passed to function. It might be that I will have 3,4,5 and so on parameters to pass. So the rule is simple - string[0] is always function name and everything else - parameters).

By the way - I've done resolving parameter values, so only need to find out the best approach how to get the function by name and pass those parameters that I got. Was thinking about delegates, but not sure if it's the best approach.

Thanks!
Posted

1 solution

Use reflection - Dynamically Invoke A Method, Given Strings with Method Name and Class Name[^].

Here is another simple example - C# GetMethod[^].
 
Share this answer
 
Comments
CLilium 13-Feb-15 7:16am    
Even though this answer is right concerning the question, shouldn't such approach be avoided? I believe there is definitely better way of doing this than using strings.
MK-Gii 13-Feb-15 7:25am    
Well this stuff is already in database so I get that string representation. And being very specific - database is responsible for specific PDF file formation and all PDF file creation logic is in DB. I have encountered a specific situation, when existing implementation limits of use of complex PDF creation aspects like looping and so on - so the only reasonalbe way out I see to pass complex expression to the database and handle that in the code, when it comes. Any other thought or suggestion would be appreciated (I would frankly be happy to avoid reflecting things too)
CLilium 13-Feb-15 8:23am    
Sorry, I should have worded it better. I understand that you will have to use strings to call certain methods, but my point it whenever there is better way than using reflection.

I know there is switch/case approach, which I think is safer and a bit faster, but ugly and probably harder to extend. While reflection is a bit slower, elegant, but can cause exceptions if the input is wrong.
I'm just wondering which one is better or whenever there is new better approach to this.
MK-Gii 13-Feb-15 8:38am    
Good idea.... I could index my functions and call depending on what index comes from the database.... gona think about the approach and well see how it looks.
Thanks for the great hint!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900