I need to learn how to convert type to Class in C#.
In my case, I have an double array object but they are stored in object array.
public object Convert(object[] values, Type targetType, object parameter,
CultureInfo culture)
{
if (values == null || values.Any(v => v == DependencyProperty.UnsetValue))
return false;
var typeOfArray = values[0].GetType();
var firstItem = (double)values[0];
return values.Skip(1).Any(item => (double)item == firstItem);
}
I want to make this generic implementation for any type but equals(method and operator)
doesn’t work.
But if I convert type to class I think I can do what I want.
But I want to learn ideas and how should I do this operation or How can I convert type object to Class to use it conversion operation?
Three options;
1: use the
object.Equals(x,y)method (this uses the virtualEquals(x)method, but avoids a null-ref problem)2: use
dynamic, but realise this may impact performance:3: use generics (here,
valuesis a typed,T[]):The nice thing about the generics option is that you avoid the need to box when it is a value-type (such as
double).