That looks like a solid way to do it.
My only suggestion is that if you're really concerned about performance with it, you may want to add generic versions for several common cases (ie. probably 1-4 args). That way, for those objects (which are most likely to be small, key-style composite objects), you won't have the overhead of building the array to pass to the method, the loop, any boxing of generic values, etc. The call syntax will be exactly the same, but you'll run slightly more optimized code for that case. Of course, I'd run some perf tests over this before you decide whether it's worth the maintenance trade-off.
Something like this:
public static int GetHashCodeFromFields<T1,T2,T3,T4>(this object obj, T1 obj1, T2 obj2, T3 obj3, T4 obj4) {
int hashCode = _seedPrimeNumber;
if(obj1 != null)
hashCode *= _fieldPrimeNumber + obj1.GetHashCode();
if(obj2 != null)
hashCode *= _fieldPrimeNumber + obj2.GetHashCode();
if(obj3 != null)
hashCode *= _fieldPrimeNumber + obj3.GetHashCode();
if(obj4 != null)
hashCode *= _fieldPrimeNumber + obj4.GetHashCode();
return hashCode;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…