Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: C# 动态运算符
我不知道这是否可能,但让我问一下:
例如,我从列表中生成一个简单的数学运算
如
List lstMat={=,+,-}
然后我生成一个介于 0-2 之间的随机值并从该列表中选择该运算符
int ir1=1; int ir2=2; int irNew= ir1 lstMat[1] ir2 ; //irNew would be 3
这可能吗?
我能想到的最接近的事情
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);