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.
You're looking for the
ObjectIDGenerator
class, which does exactly what you're trying to do.