In my current code, I'm testing the type of an object with if/else if and is :
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is double) { //do something } else if (value is int) { //do something } else if (value is string) { //do something } else if (value is bool) { //do something } Type type = value.GetType(); throw new InvalidOperationException("Unsupported type [" + type.Name + "]"); }
Instead of having a long list of else if , I tried to condense all the is statements using the Extension Method , but to no avail.
Here is my attempt in the Extension Method :
public static class Extensions { public static bool Is<T>(this T t, params T[] values) { return values.Equals(t.GetType()); } }
and method:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is double) {
Does anyone know why this fails? Any help would be greatly appreciated!
source share