This is from a related (but subtly different) question, but I think the answer is pretty tidy - so I thought it warranted adding here too.
Here's an option that uses Expression trees to discuss our intent, capturing the value at each node - allowing a single release:
static class ComExample {
static void Main()
{
using (var wrapper = new ReleaseWrapper())
{
var baz = wrapper.Add( () => new Foo().Bar.Baz );
Console.WriteLine(baz.Name);
}
}
}
class ReleaseWrapper : IDisposable
{
List