1

Possible Duplicate:
C# dynamic operator

I don't know if this is possible but let me ask:

For example I generate a simple math operation from a list

such as

List lstMat={=,+,-}

Then I generate a random value between 0-2 and select that operator from that list

such as

    int ir1=1;
    int ir2=2;

    int irNew=    ir1 lstMat[1] ir2 ;
    //irNew would be 3

Is this possible?

Community
  • 1
  • 1
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • Yes, it is possible. Does that answer your question? – dtb Oct 20 '12 at 22:12
  • @dtb, well, the OP can certainly design code that accomplishes his objective (in general terms), but there's no way `ir1 lstMat[1] ir2` will ever compile. – Kirk Woll Oct 20 '12 at 22:14

1 Answers1

4

The closest thing I can think of

List<Func<int, int, int>> lstMat = new List<Func<int, int, int>>()
{
    (x,y)=>x.CompareTo(y),
    (x,y)=>x+y,
    (x,y)=>x-y
};

int ir1=1;
int ir2=2;

int irNew= lstMat[1](ir1,ir2);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
L.B
  • 114,136
  • 19
  • 178
  • 224