private bool Compare(object o1, object o2) { if (o1 == null) { if (o2 == null) { return true; } else { return false; } } else if (o1 != null && o2 != null) { return DataType(o1, o2); } else { if (o1 == null && o2 != null&&o2.ToString()=="") { return true; } if (o2 == null && o1 != null && o1.ToString()=="") { return true; } return o1.Equals(o2); } } //GetType性能比较低 private bool DataType(object o1, object o2) { try { string type = o1.GetType().Name; switch (type) { case "Int32": return Convert.ToInt32(o1) == Convert.ToInt32(o2); case "Double": return Convert.ToDouble(o1) == Convert.ToDouble(o2); case "Decimal": return Convert.ToDecimal(o1) == Convert.ToDecimal(o2); case "Single": return Convert.ToSingle(o1) == Convert.ToSingle(o2); case "Char": return Convert.ToChar(o1) == Convert.ToChar(o2); case "String": return o1.ToString() == o2.ToString(); case "Boolean": return Convert.ToBoolean(o1) == Convert.ToBoolean(o2); case "Int16": return Convert.ToInt16(o1) == Convert.ToInt16(o2); case "Int64": return Convert.ToInt64(o1) == Convert.ToInt64(o2); default: return o1 == o2; } } catch { return false; } }
//现在进行改良
private bool DataType(object o1, object o2) { try { IConvertible conv = o1 as IConvertible; if (conv != null) { switch (conv.GetTypeCode()) { case TypeCode.Boolean: break; case TypeCode.Byte: break; case TypeCode.Char: break; case TypeCode.DBNull: break; case TypeCode.DateTime: break; case TypeCode.Decimal: break; case TypeCode.Double: break; case TypeCode.Empty: break; case TypeCode.Int16: break; case TypeCode.Int32: break; default: break; } } else { } } catch { return false; } }