Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have this string that holds a function:

C#
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:

C#
static void Main(string[] args)
        {
            Type type1 = typeof(Class1);
            //Instance
            object obj = Activator.CreateInstance(type1);
            object[] mParam = new object[] { 5, 10 };
            //invoke method, pass mParam to selected code
            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:

C#
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);
            //Instance
            object obj = Activator.CreateInstance(type1);
            object[] mParam = new object[] { 5, 10 };
            //invoke method, pass mParam to selected code
            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.
Posted
Updated 17-Mar-13 18:02pm
v7

1 solution

No, not just a function. Isn't it obvious? It would not make sense because the context of such function would be unknown.

You can do such things if provide valid source code for whole assembly and referenced assemblies if applicable. This is done via CodeDOM:
http://msdn.microsoft.com/en-us/library/hh156524.aspx[^],
http://msdn.microsoft.com/en-us/library/f1dfsbhc.aspx[^].

Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

[EDIT]

And please don't re-post. If you have further questions, comment existing solutions or add clarifications to your questions using "Improve question" (see above).

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900