0

I am writing a text file parser for a specific matching condition, and in a couple of the files I need to do some custom manipulation. What I would like to do is store the name of the custom procedure that is being used in an external XML file with the other rules. Most won't use this, and I found this answer regarding the action call:

Variable for function names

The above has the following dictionary definition

private static readonly IDictionary<string,Action<string>> actionByType =
    new Dictionary<string,Action<string>> 

Element adding from XML file in my current program (These two elements will be added)

foreach (XmlNode node in nodes)
            {
                Client holding = new Client();

                holding.has_custom = 
                     Convert.ToBoolean(
                      node.SelectSingleNode("has_custom").InnerText);
                holding.custom_call = 
                     node.SelectSingleNode("custom_call").InnerText;

                clients.Add(holding);
            }

As I go through this, how do I assign the name of the custom call as an action to be called in the dictionary? And then do I use a case statement with the generic parse as the default?

2
  • I'm not sure what you're asking. In your example, actionByType is a dictionary. What are you proposing as an alternative? Commented May 22, 2014 at 18:59
  • @JLRishe - Edited and clarified I believe. Commented May 22, 2014 at 19:09

2 Answers 2

1

Im not sure if I understand you correctly, but you can assign Actions / functions (Delegates to be more specific) like this:

actionByType.Add("write", text => Console.WriteLine(text));
actionByType.Add("write2", Console.WriteLine);

or like this:

void someAction(string someString)
{
     Console.WriteLine(someString);
}

...
actionByType.Add("write", someAction);

Then Invoke like this:

actionByType["write"]("Hello World!");

So in your case it would be:

actionByType[holding.custom_call]("What ever you need that string argument for");

Here is the fiddle https://dotnetfiddle.net/oFuEeF

Sign up to request clarification or add additional context in comments.

Comments

0

First, get the proper MethodInfo using reflection. This should be a static method, and should reside in a static class containing all your XML-accessible methods.

var method = typeof(MyStoredTypes).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static)

You'll also need a ParameterExpression to capture the incoming string.

var param = Expression.Parameter(typeof(string));

Finally, System.Linq.Expression.Call to create the Expression tree for your call, Lambda it, and Compile it.

var act = Expression.Lambda<Action<string>>(
    Expression.Call(param, method),
    new ParameterExpression[] { param })
.Compile();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.