Hi, I have this string that holds a function:
public string func = @"public int Add(int x, int y)
{
int z;
if ((x == 10) || (y == 20))
{
z = x + y;
}
else
{
z = x;
}
return z;
}";
And I want to call it in my main method to be executed:
static void Main(string[] args)
{
Type type1 = typeof(Class1);
object obj = Activator.CreateInstance(type1);
object[] mParam = new object[] { 5, 10 };
int res = (int)type1.InvokeMember("func", BindingFlags.InvokeMethod, null, obj, mParam);
Console.Write("Result: {0} \n", res);
Console.ReadKey();
}
The whole Code looks like this:
class Class1
{
public string func = @"public int Add(int x, int y)
{
int z;
if ((x == 10) || (y == 20))
{
z = x + y;
}
else
{
z = x;
}
return z;
}";
static void Main(string[] args)
{
Type type1 = typeof(Class1);
object obj = Activator.CreateInstance(type1);
object[] mParam = new object[] { 5, 10 };
int res = (int)type1.InvokeMember("Multiply", BindingFlags.InvokeMethod, null, obj, mParam);
Console.Write("Result: {0} \n", res);
Console.ReadKey();
}
}
So I was wondering if there is a way to execute the function from my string func in my main method.
Thanks.
If I will pass some values in that function inside a string? how will I do that and how will I execute that? Sorry if i have a lot of questions.