vendredi 29 janvier 2016

Overriding values c#

I have a situation where in I have 3 classes like below:

public class A
{
    public int Id { get; set; }
    public int BranchId { get; set; }
    public int LocationId { get; set; }
    public int DepID { get; set; }
    public int Age { get; set; }
    public string Name { get; set; }
}

public class B
{
    public int LocationId { get; set; }
    public int DepID { get; set; }
    public int Age { get; set; }
    public string Name { get; set; }
}  

public class C
{
    public int Age { get; set; }
    public string Name { get; set; }
}

From the above classes we see that there are few common properties b/w 3 classes.

I have a situation where I have to create a final output by combining the values from 3 classes and overriding them when there and values in lower classes.

For ex:-

Consider property Age which is all 3 classes A, B and C but if the value of it in C is not null while merging the value in class C takes priority, and this will be the case for all the properties.

I have already achieved this using Reflection, by reading the values from the instance of all the 3 classes I have by using .GetType() and .GetProperties() methods which are part of System.Reflection library.

With the current Reflection based approach I will able to handle any addition of properties in any of the classes.

But I have read that the Reflection is bad on performance.

So is there a better way of doing this?





Aucun commentaire:

Enregistrer un commentaire