Calling a function by it's name which has been stored in a string
-
Hello, I've found in every function I am writing I have lots of debug code and was just wondering if I can somehow shorten it. For example:
void testFunction() { try { .... Do something that might cause an exception here } catch(mySpecialException e) { myLogClass.log(e.message); myLogClass.dump; //dumps systeminfo to somewhere } }
What I would like is to have some kind of wrapper which I could pass the function name (either by name or as a pointer(delegate?)) to it which would then execute and return. Example:void wrapper(functionToRun()) { try { functionToRun(); } catch(...) { myLogClass.log(message); } } void test() { Console.WriteLine("Hello this is a test!"); } void hello() { Console.WriteLine("Hello"); } static void Main(string[] args) { wrapper(test()); wrapper(hello()); }
Any ideas? Thanks -
Hello, I've found in every function I am writing I have lots of debug code and was just wondering if I can somehow shorten it. For example:
void testFunction() { try { .... Do something that might cause an exception here } catch(mySpecialException e) { myLogClass.log(e.message); myLogClass.dump; //dumps systeminfo to somewhere } }
What I would like is to have some kind of wrapper which I could pass the function name (either by name or as a pointer(delegate?)) to it which would then execute and return. Example:void wrapper(functionToRun()) { try { functionToRun(); } catch(...) { myLogClass.log(message); } } void test() { Console.WriteLine("Hello this is a test!"); } void hello() { Console.WriteLine("Hello"); } static void Main(string[] args) { wrapper(test()); wrapper(hello()); }
Any ideas? ThanksIf all your functions have the same signature, then it is possible. You can do something like
public class Foo
{
delegate void WrapperDelegate();void Hello() { Console.WriteLine("Hello"); } void Test() { Console.WriteLine("Test"); } public void SomeFunc() { executeFunction(new WrapperDelegate(Hello)); executeFunction(new WrapperDelegate(Test)); } private void executeFunction(WrapperDelegate d) { try { d(); } catch(Exception e) { // blah blah } }
}
Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
If all your functions have the same signature, then it is possible. You can do something like
public class Foo
{
delegate void WrapperDelegate();void Hello() { Console.WriteLine("Hello"); } void Test() { Console.WriteLine("Test"); } public void SomeFunc() { executeFunction(new WrapperDelegate(Hello)); executeFunction(new WrapperDelegate(Test)); } private void executeFunction(WrapperDelegate d) { try { d(); } catch(Exception e) { // blah blah } }
}
Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Thanks for the suggestion but unfortunately hardly any of my functions have the same signature!
-
Thanks for the suggestion but unfortunately hardly any of my functions have the same signature!
The solution will work if there only a few signatures. You just need to create one
WrapperDelegate
for each type and add oneexecuteFunction
for each such WrapperDelegate. Regards Senthil _____________________________ My Blog | My Articles | WinMacro