I am developing a class library.
One utility method is called frequently and is being passed a complex third party object. The method uses data from the model that is time consuming to gather. Thus, for performance reasons, I would like to cache the data into a Dictionary class so that the necessary data is gathered once and then made available for all consecutive calls.
However, for several reasons it is not convenient to demand that the consumer of the library maintain an object instance where the dictionary could be stored.
Is it possible to create an object that can be accessed like a static field, but which would be collected if another (designated) object is collected?
Quasi code:
SomeMethodRequiringALotOfDataFromHugeSystem(detector detectorInHugeSystem)
{
//access HugeSystem from a property in the dectector.
//find if Huge System already has a Dictioary.
//Build a Dictionary from data in Huge system if necessary.
//(Not possible?) Associate the dictionary to the HugeSystem object instance so that Dictionary will be collected if HugeSystem instance is collected.
// Perform time efficient task based on Dictionary.
}