I’m trying to use the following by calling: StaticClass.DeleteObject(null, key). It didn’t work as I expected, and the code in the lambda expression throws a NullReferenceException because context is null, which is understandable. I tried changing the context parameter of AmazonS3Operation to a ref, but ReSharper then warns of “Access to modified closure.”
Basically I’m just trying to “inject” the lambda expression in that helper method, then call it, if you get the idea of how I’d like it to work. Is this possible, or what could I do that’s similar if not?
/// <exception cref="IOException"></exception>
public static void DeleteObject(this AmazonS3Context context, string key)
{
AmazonS3Operation(context, () => context.Client.DeleteObject(
new DeleteObjectRequest().WithBucketName(context.CurrentBucket)
.WithKey(key)));
}
/// <exception cref="IOException"></exception>
private static void AmazonS3Operation(AmazonS3Context context, Action operation)
{
var shouldDispose = false;
try
{
if (context == null)
{
context = new AmazonS3Context();
shouldDispose = true;
}
operation();
}
catch (AmazonS3Exception e)
{
throw new IOException(e.Message, e);
}
finally
{
if (shouldDispose)
context.Dispose();
}
}
I would pass the context into the action. Replace the Action parameter with an Action<AmazonS3Context> parameter.