dimanche 8 mars 2015

C# Reflection How to compare two objects and get different values using Properties

This is basically what I'm trying to do. I have a class PeopleGuesses which contains two fields, objectKey, objectValue. Basically these two fields will contain exactly that, the objectKey is the Field Property name, and the value is that, the value. What i'm trying to do is basically return the class MurderScene with all the values of the original class and then all the values that are different in the PeopleGuesses class.


Basically below, when I get out of the MurderScene SetMurderScene() I want the entire class of originalScene with the only difference of MyItem4 value being Plum.


Here is the code i'm working with, which should compile with no issue.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ReflectionCreateUpdate
{
class Program
{
public
static void Main(string[] args)
{
//stop watch for performance testing
Stopwatch sw = new Stopwatch();
sw.Start();
//got to set the original murder scene for guesses
MurderScene originalScence = SetMurderScene();

//this is everybodys guess
List<PeopleGuesses> allGuessItems = new List<PeopleGuesses>();
//myItems 1 -5
string[] ObjectKeys = { "MyItem1", "MyItem2", "MyItem3", "MyItem4", "MyItem5" };
string[] ObjectValues = { "Target", "Lead Pipe", "Ball Room", "Plum", "Check" };
for (int i = 0; i < 5; i++)
{

PeopleGuesses peepGuess = new PeopleGuesses();
peepGuess.objectKey = ObjectKeys[i];
peepGuess.objectValue = ObjectValues[i];
allGuessItems.Add(peepGuess);
}


//this is what i'm unsure of how to check object key vs property of MurderScene
diffMurderScene(allGuessItems, originalScence);

Console.WriteLine(sw.Elapsed);
Console.ReadLine();

}

public static MurderScene SetMurderScene()
{
MurderScene mur = new MurderScene();
mur.MyItem1 = "Target";
mur.MyItem2 = "Lead Pipe";
mur.MyItem3 = "Ball Room";
mur.MyItem4 = "Mustard";
mur.MyItem5 = "Check";
return mur;
}

public static MurderScene diffMurderScene(List<PeopleGuesses> allGuessItems, MurderScene murderScene)
{
MurderScene myNewMurderScene = murderScene;

//if objectkey == murderscene property && objectValue != murderscene propertyValue
//set MurderScene object property with new value with new value

return myNewMurderScene;

}
}
public class MurderScene
{
public string MyItem1 { get; set; }
public string MyItem2 { get; set; }
public string MyItem3 { get; set; }
public string MyItem4 { get; set; }
public string MyItem5 { get; set; }
}

public class PeopleGuesses
{
public object objectKey { get; set; }
public object objectValue { get; set; }

}
}





Aucun commentaire:

Enregistrer un commentaire