In my code I’m instantiating a legacy Delphi object through a COM interface.
This class needs to be instantiated many times, so in order to lower the overhead of instantiating it I cache it at a point where 70% of all the calls have the common resulting object.
However, when I alter the object after it is cached, the changes are persisted in the cache too. This makes me think the COM wrapper instance is passed by ref instead of by value.
How can I make sure that the object in cache is passed by value rather then by ref?
I think that, if it’s possible to do at all, you need to explicitly clone a copy of the object and then cache the copy. See for example the
MemberwiseClonemethod, and others mentioned in answers to Cloning objects in C#.