mardi 26 mai 2015

Checking if a class contains any properties of type double and converting them to integers

I have a fairly complex model class that I need to compare to another class of the same type. I've already implemented a compare function using reflection, but one of the classes will have values rounded to the integer, and the other will have values as doubles. I need to round all of those double values to the nearest integer, in order for my comparison function to work.

The Compare function in question:

public static List<Variance> DetailedCompare<Type>(this Type saveModel, Type loadModel)
{

    List<Variance> variancesList = new List<Variance>();
    PropertyInfo[] fieldList = saveModel.GetType().GetProperties();
    foreach (PropertyInfo field in fieldList)
    {
        if (!ignoreList.Contains(field.Name))
        {
            Variance variance = new Variance();
            variance.property = field.Name;
            variance.saveValue = field.GetValue(saveModel, null);
            variance.loadValue = field.GetValue(loadModel, null);

            if (!Equals(variance.saveValue, variance.loadValue))
                variancesList.Add(variance);
        }
    }
    return variancesList;
}

The model class I need to compare:

public class DisplayModel
{
    public Point topLeft { get; set; }
    public Point bottomRight { get; set; }
    public double? intercept { get; set; }
}

public class Point
{
    public double x { get; set; }
    public double y { get; set; }
}

Is there a way to iterate through the object properties and check if they're of type double, or would changing each one manually be neccessary?





Aucun commentaire:

Enregistrer un commentaire