C#,如何将int的增量声明为对象?

tes*_*der 3 .net c# types casting

我有一个外部系统,它给我一个对象值(我知道这个值总是一个盒装整数类型).我想以通常的方式增加它:int value += otherIntValue,但我从编译器得到一个错误:

运算符'+ ='不能应用于类型的操作数

例如:

//source values i cannot to change it
object targetInt = 100;
int incrementedValue = 20;

//usual way - not works
targetInt += incrementedValue;    

//ugly workaround
targetInt = ((int) targetInt) + incrementedValue;
Run Code Online (Sandbox Code Playgroud)

有没有办法增加int和object的实例targetInt += incrementedValue;

ken*_*n2k 6

只是不要改变你的代码.将您object转换为整数是完全正确的,因此可以使用另一个整数进行添加.