Converting a variable to a method so it can be run (somewhat like Eval could do).
-
Can someone help me!? :) I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...! public static void RunAMethod(string methodname) { methodname(); //method to run } ///---- so I could do this from another method RunAMethod("runwhatevermethodIwant"); ///----- Is this possible??? :sigh: :(
-
Can someone help me!? :) I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...! public static void RunAMethod(string methodname) { methodname(); //method to run } ///---- so I could do this from another method RunAMethod("runwhatevermethodIwant"); ///----- Is this possible??? :sigh: :(
Read about Reflection in .NET. http://msdn2.microsoft.com/en-us/library/f7ykdhsy(VS.80).aspx[^] In this page, you will see a paragrah beginning with Use MethodInfo to discover information .... This paragraph is your friend.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
-
Can someone help me!? :) I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...! public static void RunAMethod(string methodname) { methodname(); //method to run } ///---- so I could do this from another method RunAMethod("runwhatevermethodIwant"); ///----- Is this possible??? :sigh: :(
Reflection is way slow. You should consider to use delegates, which are quite similar to C's function pointers. The topic is not very simple, but delegates are very powerful and elegant. Here[^] is an introductory article on MSDN Magazine.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki
-
Can someone help me!? :) I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...! public static void RunAMethod(string methodname) { methodname(); //method to run } ///---- so I could do this from another method RunAMethod("runwhatevermethodIwant"); ///----- Is this possible??? :sigh: :(
Thank you very much for both of your replies, I had a hunch that Reflection might be something I needed to look into, and I'll definitely give it a shot. :-)
-
Reflection is way slow. You should consider to use delegates, which are quite similar to C's function pointers. The topic is not very simple, but delegates are very powerful and elegant. Here[^] is an introductory article on MSDN Magazine.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki
Delegates seem very interesting to me also...although I am having a little difficulty visualizing their usage properly from the MSDN article, is there anyway you could write out a little snippet showing how I could use delegates to call a method whose name is based on a string I enter? :) Thanks!
-
Reflection is way slow. You should consider to use delegates, which are quite similar to C's function pointers. The topic is not very simple, but delegates are very powerful and elegant. Here[^] is an introductory article on MSDN Magazine.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki
Can you call a delegate with a method name defined in a string?!? Because this seems to be what he wants.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
-
Can you call a delegate with a method name defined in a string?!? Because this seems to be what he wants.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
No. Not directly at least. The point is that invoking methods at runtime by their name is not very clean, in my opinion. Since I guess the methods he wants to invoke are parameterless and with no return value, using delegates would be very easy and would improve the robustness of the code.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki
-
No. Not directly at least. The point is that invoking methods at runtime by their name is not very clean, in my opinion. Since I guess the methods he wants to invoke are parameterless and with no return value, using delegates would be very easy and would improve the robustness of the code.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki
I agree with you in the way that he should consider using an interface instead instead of calling a method by a name specified in a string.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown