2

Is it possible to pass a function (like let's say sin() ) via string and then use it as int?

Like: (main idea only)

public int getfunc(String func)
{
   return res_of(func)
}

I tried playing around with string of "Math.sin(0)" but couldn't print the 0...

I could predefine the math functions since I only need 1 and then it becomes extremely simple as I only pass the int value for the function to work on, but I thought may-hap there is a way to keep it more generic.

I do not want to use mapping of the functions I want to keep it dynamic.... is ther a way of doing so?

6
  • 2
    You mean, you want to pass a string and execute that string in the code ? Commented Nov 21, 2013 at 10:33
  • 2
    so you do not want to use Actions and Funcs? Commented Nov 21, 2013 at 10:34
  • Maybe you should provide an example of how you intend on using this. Maybe there is a better way, or at least is will help us understand your needs a bit more Commented Nov 21, 2013 at 10:35
  • 1
    You most probably want to use delegates. It's unlikely that you really need to compile a string expression in runtime. Commented Nov 21, 2013 at 10:37
  • 1
    Please provide usage example(s) too... Commented Nov 21, 2013 at 10:39

4 Answers 4

3

I'd like to offer an alternative approach that you may not have considered.

You could use a delegate instead of passing a string; that way, you won't need any reflection.

There's a predefined delegate type in C# called Func<> which lets you easily define the return type and parameter types of a method that you want to pass as a delegate.

For example, the Func<> for Math.Sin(double) would be Func<double, double> because Math.Sin() returns a double and takes a double parameter.

An example will make this clearer:

using System;

namespace Demo
{
    internal class Program
    {
        private void run()
        {
            Func<double, double> f1 = Math.Sin;
            Func<double, double> f2 = Math.Cos;

            double r1 = runFunc(f1, 1.0);
            double r2 = runFunc(f2, 2.0);

            Console.WriteLine(r1);
            Console.WriteLine(r2);
        }

        private static double runFunc(Func<double, double> func, double parameter)
        {
            return func(parameter);
        }

        private static void Main()
        {
            new Program().run();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try using http://www.csscript.net/

 dynamic script = CSScript.Evaluator
                         .LoadCode(@"using System;
                                     public class Script
                                     {
                                         public int Sum(int a, int b)
                                         {
                                             return a+b;
                                         }
                                     }");
int result = script.Sum(1, 2);

1 Comment

Unfortunately we are not allowed to use scripts since we did not learn scripts :( Regardless I coudln't get it to work. I will play around with it , as for it's a new way of thinking though I did not learn any of these tools.
1

Declare the method like this:

public int DoCalculation(Func<double, double> func, double a)
    {
        return Convert.ToInt32(func(a));
    }

Then use it like this:

int result = DoCalculation(Math.Sin, 3.3);

Comments

0

In our application we use the .NET integrated C# compiler.
This is some work to do but straight-forward to implement.

Here's an answer with a lot more details on that.
We use this in our companies production.

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.