Skip to content

Instantly share code, notes, and snippets.

@gro-ove
Created April 26, 2017 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gro-ove/87867496fede43818a490808d608f688 to your computer and use it in GitHub Desktop.
Save gro-ove/87867496fede43818a490808d608f688 to your computer and use it in GitHub Desktop.
public static class ConverterHelper {
public static int AsInt([CanBeNull] this object value) {
return value.AsInt(0);
}
public static int AsInt([CanBeNull] this string value) {
return value.AsInt(0);
}
public static int AsInt([CanBeNull] this object value, int defaultValue) {
return value as int? ?? value?.ToString().AsInt(defaultValue) ?? defaultValue;
}
public static int AsInt([CanBeNull] this string value, int defaultValue) {
int result;
if (value == null || !int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {
return defaultValue;
}
return result;
}
public static double AsDouble([CanBeNull] this object value) {
return value.AsDouble(0);
}
public static double AsDouble([CanBeNull] this string value) {
return value.AsDouble(0);
}
public static double AsDouble([CanBeNull] this object value, double defaultValue) {
return value as double? ?? value?.ToString().AsDouble(defaultValue) ?? defaultValue;
}
public static double AsDouble([CanBeNull] this string value, double defaultValue) {
double result;
if (value == null || !double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {
return defaultValue;
}
return result;
}
public static bool AsBoolean([CanBeNull] this object value) {
return value.AsBoolean(false);
}
public static bool AsBoolean([CanBeNull] this string value) {
return value.AsBoolean(false);
}
public static bool AsBoolean([CanBeNull] this object value, bool defaultValue) {
return value as bool? ?? value?.ToString().AsBoolean(defaultValue) ?? defaultValue;
}
public static bool AsBoolean([CanBeNull] this string value, bool defaultValue) {
bool result;
if (value == null || !bool.TryParse(value, out result)) {
return defaultValue;
}
return result;
}
public static bool XamlEquals([CanBeNull] this object properValue, [CanBeNull] object xamlValue) {
if (properValue == null) return xamlValue == null;
if (xamlValue == null) return false;
if (properValue is double) {
return Equals((double)properValue, xamlValue.AsDouble());
}
if (properValue is int) {
return Equals((int)properValue, xamlValue.AsInt());
}
if (properValue is bool) {
return Equals((bool)properValue, xamlValue.AsBoolean());
}
if (properValue is string) {
return Equals((string)properValue, xamlValue.ToString());
}
return Equals(properValue, xamlValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment