views:

52

answers:

3

In one of my project i need to call methods dynamicly depending upon the regular expression a string. in one of my case i have this string.

And(Answered(ARef(16350,100772,null)),Not(AnyOf(ARef(16350,100772,null),[Closed temporarily])),Not(AnyOf(ARef(16350,100772,null),[Closed down])))

which will like this if arrange it to understand .

  And(
            Answered(
                    ARef(
                    16350,
                    100772,
                    null)
                    ),
                    Not(
                        AnyOf(
                            ARef(
                                16350,
                                100772,
                                null),              
                            [Closed temporarily]
                            )
                       ),
                    Not(
                       AnyOf(
                            ARef(
                                16350,
                                100772,
                                null),
                        [Closed down]
                            )
                      )
        )

is there any way to call methods which are started by methodname and ( "Open bracket" parameters and ) "closing brackits"

in above case the And is a method which take parameters from the answered method. and so on....

Please suggest me to find a way to do this.

+1  A: 

This isn't a regex question. You need to implement a parser/interpreter for the language that's represented by your string. There are many parser libraries/tools for C# that can help you here.

See this list of possibly relevant Stackoverflow questions and answers

Paul
I agree. Solving this using regex might be possible but will be extremely complicated. Creating a parser will make your solution much better.
Emile
A: 
  • I don't know what your attempting to do here but its not a fantastic design idea. If you need to supply this extent of extendability why not use MEF or allow for assemblies to by loaded on the fly.

However you can do what you are attempting using reflection. You can even create an on the fly assembly and then run it.

An example of dynamically loading and calling an object is as follows.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ca1
{

public class A
{
    public int retval(int x)
    {
        return x; 
    }
}


class Program
{

    static void Main(string[] args)
    {
        string s ="retval";
        Type t = typeof(A);
        ConstructorInfo CI = t.GetConstructor(new Type[] {});
        object o = CI.Invoke(new object[]{});
        MethodInfo MI = t.GetMethod(s);
        object[] fnargs = new object[] {4};
        Console.WriteLine("Function retuned : " +MI.Invoke(o, fnargs).ToString());

      }
   }
}
rerun
+1  A: 
public static string InvokeStringMethod(string typeName, string methodName)
{
// Get the Type for the class
Type calledType = Type.GetType(typeName);

// Invoke the method itself. The string returned by the method winds up in s
String s = (String)calledType.InvokeMember(
                methodName,
                BindingFlags.InvokeMethod | BindingFlags.Public | 
                    BindingFlags.Static,
                null,
                null,
                null);

// Return the string that was returned by the called method.
return s;

}

References:http://dotnetacademy.blogspot.com/2010/10/invoke-method-when-method-name-is-in.html

http://www.codeproject.com/KB/cs/CallMethodNameInString.aspx

Rajesh Rolen- DotNet Developer