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?

vgru
  • 49,838
  • 16
  • 120
  • 201
user2893825
  • 95
  • 2
  • 9

4 Answers4

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();
        }
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
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);
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • 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. – user2893825 Nov 21 '13 at 11:17
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);
Marton
  • 813
  • 7
  • 18
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.

Community
  • 1
  • 1
joe
  • 8,344
  • 9
  • 54
  • 80