R
Ruediger Klaehn
I have written an arithmetic expression compiler. Now that it is finished I
would like to give it a nice interface. Currently I have this interface:
delegate double Function(double x);
class ArithmeticExpression
{
public static Function Compile(string text);
...
}
This can be used like this:
Function f=ArithmeticExpression.Compile("x*x");
Console.WriteLine(f(2)); //gives 4
Obviously this only supports one parameter, which is always of type double
and called x.
I would like to do a similar interface for functions with multiple
parameters, but I do not want to have to define a delegate for each number
of parameters. So I would like to do it like this:
class ArithmeticExpression
{
public static Delegate Compile(string text,Type delegateType);
}
This could then be used like this:
delegate double Function2(double x,double y);
Function2 g=ArithmeticExpression.Compile("x*y",typeof(Function2));
Console.WriteLine(g(1,2));
To do it that way I need to get the number and type of parameters as well as
the return type from the delegate type. I looked at System.Type, but found
no obvious way to do this.
Is this possible? Or is there a better way to achieve a simple syntax like
the one shown above?
best regards
Rüdiger
would like to give it a nice interface. Currently I have this interface:
delegate double Function(double x);
class ArithmeticExpression
{
public static Function Compile(string text);
...
}
This can be used like this:
Function f=ArithmeticExpression.Compile("x*x");
Console.WriteLine(f(2)); //gives 4
Obviously this only supports one parameter, which is always of type double
and called x.
I would like to do a similar interface for functions with multiple
parameters, but I do not want to have to define a delegate for each number
of parameters. So I would like to do it like this:
class ArithmeticExpression
{
public static Delegate Compile(string text,Type delegateType);
}
This could then be used like this:
delegate double Function2(double x,double y);
Function2 g=ArithmeticExpression.Compile("x*y",typeof(Function2));
Console.WriteLine(g(1,2));
To do it that way I need to get the number and type of parameters as well as
the return type from the delegate type. I looked at System.Type, but found
no obvious way to do this.
Is this possible? Or is there a better way to achieve a simple syntax like
the one shown above?
best regards
Rüdiger