考慮這些類:獲取父類的散列碼
public class BaseClass {
public int SomeInt { get; set; }
public bool SomeBool { get; set; }
public BaseClass(int i, bool b) {
SomeInt = i;
SomeBool = b;
}
}
public class InheritedClass : BaseClass {
public List<Int32> SomeInts { get; set; }
public InheritedClass(int i, bool b, List<Int32> ints): base(i, b) {
SomeInts = ints;
}
public InheritedClass(BaseClass b, List<Int32> ints) : base(b.SomeInt, b.SomeBool) {
SomeInts = ints;
}
}
而且我得到了InheritedClass
一個例子,我想它的哈希碼值,如果它是一個BaseClass
。我試過澆鑄:
BaseClass x = new BaseClass(10, true);
Console.WriteLine("HashCode x: {0}", x.GetHashCode());
InheritedClass iX = new InheritedClass(x, new List<int> { 1, 2, 3 });
Console.WriteLine("HashCode iX: {0}", iX.GetHashCode());
BaseClass bX = (BaseClass)iX;
//I've also tried:
//BaseClass bX = ix as BaseClass;
Console.WriteLine("Base HashCode bX: {0}", bX.GetHashCode());
但最終WriteLine
正在恢復的哈希碼,如果它是一個InhertiedClass
。我無法覆蓋的InheritedClass
,並保存構建一個新的BaseClass
從對象的屬性,有無論如何得到我需要的HashCode?