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;?