Given the following code, I am trying to produce a unique key or hashcode for a set of similar parameters.
using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
public class Program
{
public static void Main()
{
var f = new F();
var first = f.GetHashCode<Person>(12, x => x.Name == "John");
var second = f.GetHashCode<Person>(12, x => x.Name == "John");
var third = f.GetHashCode<Person>(12, x => x.Name == "Mike");
Console.WriteLine(String.Format("{0}={1}", first, second));
Console.WriteLine(String.Format("Not Match {0}", third));
}
public class Person
{
public string Name {get; set;}
}
public class F
{
public int GetHashCode<T>(int id, params Expression<Func<T, object>>[] includes) where T: class, new()
{
// TODO: what kind of magic goes here
int hashCode = id;
foreach(var e in includes){
var code = e.GetHashCode();
hashCode *= code;
}
return hashCode;
}
}
}
In the example above, first
and second
should match, while third
should not.
Aucun commentaire:
Enregistrer un commentaire