4

We are rolling a custom serialisation tool into our project and are coming across a need to uniquely identify each object by a primitive value type that can be used as a synonym of its 'identity'. The value need have no meaning or organisation, merely that it is unique and persistent at least for the duration of a serialisation routine. Ideally .net would have internally maintained a unique and persistent object ID that we could store as the object's ID. I know every object has a GetHashCode method, but we are not confident in the value's global uniqueness. Another criteria on accessing such an ID is that the method needs to be very efficient.

I have looked at GetHashCode, the garbage collector and marshalling for ideas, haven't come to anything solid yet.

J Collins
  • 2,106
  • 1
  • 23
  • 30

1 Answers1

4

You're looking for the ObjectIDGenerator class, which does exactly what you're trying to do.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Is this based on an Object>Integer dictionary? – J Collins Oct 15 '12 at 14:52
  • Actually it appears to be based on a Hashtable, does this imply the object's `GetHashCode` method is fundamentally responsible for ID uniqueness? – J Collins Oct 15 '12 at 15:10
  • Further research, as per http://stackoverflow.com/questions/7789430/objectidgenerator-implementation the `ObjectIDGenerator` uses the `RuntimeHelpers.GetHashCode(Object)` method, which by http://msdn.microsoft.com/en-us/library/system.runtime.serialization.objectidgenerator.aspx is using a `HashTable` and then by http://msdn.microsoft.com/en-us/library/system.runtime.serialization.objectidgenerator.aspx uses the `Object.GetHashCode` method which itself by http://stackoverflow.com/questions/7458139/net-is-type-gethashcode-guaranteed-to-be-unique is not necessarily unique. – J Collins Oct 15 '12 at 20:27