int、float、bool、または string として表すことができる 2 つのオブジェクトがあります。これら 2 つのオブジェクトに対して追加を実行する必要があり、結果は c# が結果として生成するものと同じになります。たとえば、1+"Foo" は文字列 "1Foo" に等しく、2+2.5 は float 5.5 に等しく、3+3 は int 6 に等しくなります。現在、私は以下のコードを使用していますが、信じられないほどやり過ぎのようです。誰かがこれを効率的に行う方法を単純化または指摘できますか?
private object Combine(object o, object o1) {
float left = 0;
float right = 0;
bool isInt = false;
string l = null;
string r = null;
if (o is int) {
left = (int)o;
isInt = true;
}
else if (o is float) {
left = (float)o;
}
else if (o is bool) {
l = o.ToString();
}
else {
l = (string)o;
}
if (o1 is int) {
right = (int)o1;
}
else if (o is float) {
right = (float)o1;
isInt = false;
}
else if (o1 is bool) {
r = o1.ToString();
isInt = false;
}
else {
r = (string)o1;
isInt = false;
}
object rr;
if (l == null) {
if (r == null) {
rr = left + right;
}
else {
rr = left + r;
}
}
else {
if (r == null) {
rr = l + right;
}
else {
rr = l + r;
}
}
if (isInt) {
return Convert.ToInt32(rr);
}
return rr;
}