public static bool IsDefaultValue(this Type type, object value)
{
if (type == null)
{
throw new ArgumentNullException("type", "type cannot be null.");
}
TypeCode typeCode = Type.GetTypeCode(type);
if (typeCode != TypeCode.Empty && typeCode != TypeCode.Object && value == null)
{
throw new ArgumentException("Cannot pass a null value when the specified type is non-nullable.", "value");
}
if (!type.IsAssignableFrom(value.GetType()))
{
throw new ArgumentException("The specified object value is not assignable to the specified type.", "value");
}
switch (Type.GetTypeCode(type))
{
case TypeCode.Boolean:
return (bool)value == false;
case TypeCode.Byte:
case TypeCode.Char:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return (long)value == 0;
case TypeCode.DateTime:
return (DateTime)value == DateTime.MinValue;
case TypeCode.DBNull:
return true;
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return (double)value == 0;
default:
return value == null;
}
}