int, float, bool 또는 string으로 표현할 수있는 두 개의 객체가 있습니다. 이 두 객체에 추가 작업을 수행해야합니다. 결과는 결과로 생성되는 것과 동일한 결과가됩니다. 예를 들어 1+ "Foo"는 문자열 "1Foo"와 같고 2 + 2.5는 float 5.5와 같고 3 + 3은 int 6과 같습니다. 현재 아래 코드를 사용하고 있지만 엄청난 잔인한 것 같습니다. 누구든지이 일을 효율적으로하기 위해 어떤 방법으로 단순화하거나 알려줄 수 있습니까?C#에서 두 객체를 결합하는 가장 효율적인 방법
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;
}
나는 당신의 디자인을 봐야 할 것입니다 .... –