Reputation: 5691
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.
Upvotes: 1
Views: 202
Reputation: 14285
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
Upvotes: 1
Reputation: 25495
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());
}
}
}
Upvotes: 0
Reputation: 41749
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
Upvotes: 1