how do you call a method based on the contents of a variable
ex.
String S = "Hello World";
String Format = "ToUpper()";
String sFormat = s.Format;
resulting in "HELLO WORLD"
This way I could at some other time pass Format = "ToLower()" or Format = “Remove(1,4)” which will delete 4 characters starting from pos 1 – in short I would like the ability to call any string method.
Could someone post a complete working solution.
The crux of the solution requires you to use Reflection to locate the required method. This is a simple example covering your sitaution;
You can make the method more generic, to accept arguments to the method you want to call, as below. Note the change to the way .GetMethod and .Invoke are called to pass the required arguments.