Hi Friends,
I need to write a generic method which will compare two objects of same type and stores all the mismatches in
List<string>
. My program flow is something like. I will post data via API call. Once i posted it will return me a unique ID of the post with the help of the ID i will call the GET method and de-serialize the response. I need to ensure that whatever i posted is am getting back so i need to compare POST object and GET object.
While comparing some of the properties should be ignored based upon the object i am passing to the method. Ignore list items i am reading from the config file.
What i tried, Loop through all the properties of the class using Reflection but i am not able to go inside of the list of objects. Please help me to modify the Compare method.
My sample class structure
class Plan
{
public int planId { get; set; }
public string plnaName { get; set; }
public List<Client> clients { get; set; }
public List<Scenario> scenarios { get; set; }
public Setting settings { get; set; }
public string verificationtimestamp { get; set; }
}
class Scenario
{
public int scenarioId { get; set; }
public string scenarioName { get; set; }
public Liablity liablities { get; set; }
public Goal goals { get; set; }
public CashFlow cashflows { get; set; }
}
class Client
{
public int clientId { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string state { get; set; }
}
class Setting
{
public int analysishorizon { get; set; }
public int iterations { get; set; }
public Dictionary<string, string> settings { get; set; }
}
class Liablity
{
public int liablityd { get; set; }
public string liablityName { get; set; }
}
class CashFlow
{
public int cashflowId { get; set; }
public string cashflowName { get; set; }
}
class Goal
{
public int goalId { get; set; }
public string goalName { get; set; }
}
enum Ignorelist
{
plan,
planclient,
scenario,
goal,
liabilities,
cashflows
}
What I have tried:
public static List<String> lsterrorlogs = new List<string>();
static void Main(string[] args)
{
Plan plnPost = new Plan
{
planId = 1,
plnaName = "Plan1",
scenarios = new List<Scenario>(){
new Scenario {scenarioId=1,scenarioName="scenario1",
liablities=new Liablity{liablityd=1,liablityName="L1"},
goals=new Goal{goalId=1,goalName="G1"},
cashflows=new CashFlow{cashflowId=1,cashflowName="CF1"}},
new Scenario {scenarioId=2,scenarioName="scenario2",
liablities=new Liablity{liablityd=2,liablityName="L2"},
goals=new Goal{goalId=2,goalName="G2"},
cashflows=new CashFlow{cashflowId=2,cashflowName="CF2"}}},
settings = new Setting { analysishorizon = 10, iterations = 100 }
};
Plan plnGet = new Plan();
plnGet = plnPost;
Compare<Plan>(plnPost, plnGet,Ignorelist.plan);
Console.ReadLine();
}
static bool Compare<T>(T PostObject, T GetObject,Ignorelist il)
{
List<string> ignoreitems = new List<string>();
switch (il)
{
case Ignorelist.plan:
ignoreitems.Add("verificationtimestamp");
ignoreitems.Add("planId");
break;
case Ignorelist.planclient:
break;
case Ignorelist.scenario:
break;
case Ignorelist.goal:
break;
case Ignorelist.liabilities:
break;
case Ignorelist.cashflows:
break;
default:
break;
}
Type type = PostObject.GetType();
foreach (PropertyInfo properties in type.GetProperties())
{
object valuePost;
object valueGet;
valuePost = GetPropValue(PostObject, properties.Name);
valueGet = GetPropValue(GetObject, properties.Name);
if (valuePost == null && valueGet != null || valuePost != null && valueGet == null)
lsterrorlogs.Add("one of the values is null");
}
return true;
}
public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}