-2

How do I dynamically pass string methods to be applied to strings at run time. ex.

Private String Formatting(String Data, String Format)

When we pass String S1 = "S1111tring Manipulation" and format = Remove(1,4) - behind the scenes it becomes S1.Remove(1,4) resulting in "String Manipulation"

or if we pass String S1 = "S1111tring Manipulation" and format = ToLower() behind the scene it becomes S1.ToLower() resulting in "s1111tring manipulation"

I should be able to pass any valid method like PadLeft(25,'0'), PadRight, Replace etc...

I would appreciate a complete example

This is what I have tried and it does not work

using System.Reflection;
string MainString = "S1111tring Manipulation";
string strFormat = "Remove(1, 4)";
string result = DoFormat(MainString, strFormat);

private string DoFormat(string data, string format)
        {
            MethodInfo mi = typeof(string).GetMethod(format, new Type[0]);
            if (null == mi)
                throw new Exception(String.Format("Could not find method with name '{0}'", format));

            return mi.Invoke(data, null).ToString();
        } 

throws an error (Could not find method with name 'Remove(1, 4)') - so I am not sure how to proceed

J. Steen
  • 15,470
  • 15
  • 56
  • 63
wadapav
  • 107
  • 5
  • 14
  • I'm not getting you right, are you looking into passing a name of a function to a function? – Shai Jul 24 '12 at 13:00
  • 1
    You -could- mark any of the answers to your previous question as correct, or improve that question instead of asking a completely new one with the same meaning. http://stackoverflow.com/questions/11623436/c-sharp-call-method-based-on-contents-of-a-variable – J. Steen Jul 24 '12 at 13:04
  • What have you tried? What do you mean, 'to be applied at runtime'? This is very vague. Everything is ultimately done at runtime. Seems like you want to pass in a string and a method to another method, is that it? Make a switch statement in your method and match the value of the format parameter and execute the corresponding method. – GrayFox374 Jul 24 '12 at 13:06

2 Answers2

2

Have a look at Reflection. You can essentially implement what you're describing using it save for the parsing of the user supplied text.

The smiple example you've used there would be something like,

var method = "ToLower()";
var methodInfo = typeof(String).GetMethod(method);
var string = "foo";
string.GetType().InvokeMember(....);
M Afifi
  • 4,645
  • 2
  • 28
  • 48
  • How would i go about implementing Remove(1,4) – wadapav Jul 24 '12 at 13:19
  • 2
    @wadapav you could use regex to remove the parameters, and then with reflection you pass the parameters as an object array. However, it would be difficult to tell what to cast the types as and you lose all compile time safety. Why are you trying to do this? – James Jul 24 '12 at 13:22
  • @James it is as James said, you'll need to use a Regex to parse the string and construct what you're after. Parse out the method name, an array of parameters, and something to infer the type of what you are being parsed. It is a lot of work if you're expecting to do this for completley free text. – M Afifi Jul 24 '12 at 13:25
  • I need to give the users the ability to decide what needs to be done with the string. I could conceivably write case statements – wadapav Jul 24 '12 at 13:40
0

Consider using an enum instead of the second string parameter. It will be helpful as for type safety.

public enum StringManipulationType
{
  ToLower,
  ToUpper
}

and then rewrite the manipulation method you had with the following:

private string Formatting(String data, StringManipulationType manipulationType)
{
  switch (manipulationType)
  {
    case StringManipulationType.ToLower:
      return data.ToLower();
    case StringManipulationType.ToUpper:
      return data.ToUpper();
    case default:
      throw new ArgumentException();
  }
}

In all the places where you had the earlier "string parameter", change it with enum like this:

Artak
  • 2,819
  • 20
  • 31
  • If the OP knows the type of the method - and they would, if they're to define an Enum - why not just use a Func and pass it in? Besides, this doesn't accurately cover the cases where they need arguments for the method. – J. Steen Jul 24 '12 at 13:13
  • J.Steen - I still don't have an answer or code that I can use for my situation. – wadapav Jul 24 '12 at 13:17
  • @wadapav And neither have you stated what you've tried, in your previous question. We do expect some effort, around here. =) – J. Steen Jul 24 '12 at 13:18
  • J.Steen - appreciate your help - please see above – wadapav Jul 24 '12 at 13:31