【发布时间】:2012-01-02 03:33:17
【问题描述】:
我正在尝试通过调用来使用以下内容:StaticClass.DeleteObject(null, key)。它没有像我预期的那样工作,并且 lambda 表达式中的代码抛出 NullReferenceException 因为上下文为空,这是可以理解的。我尝试将 AmazonS3Operation 的上下文参数更改为 ref,但 ReSharper 然后警告“访问修改后的闭包”。
基本上我只是想在那个辅助方法中“注入” lambda 表达式,然后调用它,如果你知道我希望它如何工作的话。这可能吗,或者如果没有,我能做些什么类似的事情?
/// <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();
}
}
【问题讨论】: