【问题标题】:Null reference check required to enumerate a collection inside a reflected assembly?枚举反射程序集中的集合需要空引用检查吗?
【发布时间】:2011-04-12 19:55:42
【问题描述】:

我在 C#/.NET 3.5 中遇到了一个非常奇怪的行为...

我正在编写一个与内容管理系统的上传管道挂钩的类。 CMS 通过反射执行这个钩子。

由于某些未知原因,以下代码失败抛出 NullRef(“文件”是一个 HttpFileCollection)。

foreach (var fileKey in args.Files.AllKeys)
{
     // Do Stuff
}

在语句之前添加 NullRef 检查后,循环成功。在这两种情况下,上传的文件都是相同的。 return 语句永远不会被执行,因为 null 条件失败。

if (args.Files == null ) return;
foreach (var fileKey in args.Files.AllKeys)
{
   // Do Stuff
}

我完全被这个难住了。有什么想法吗?

全栈跟踪

    ** Exception: System.Web.HttpUnhandledException **
    Message: Exception of type 'System.Web.HttpUnhandledException' was thrown.
    Source: System.Web
    at System.Web.UI.Page.HandleError(Exception e)
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    at System.Web.UI.Page.ProcessRequest()
    at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
    at System.Web.UI.Page.ProcessRequest(HttpContext context)
    at ASP.sitecore_shell_applications_flashupload_advanced_uploadtarget_aspx.ProcessRequest(HttpContext context)
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

    ** Nested Exception ** 
    Exception: System.Reflection.TargetInvocationException
    Message: Exception has been thrown by the target of an invocation.
    Source: mscorlib
    at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes,        RuntimeTypeHandle typeOwner)
    at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes,        RuntimeTypeHandle typeOwner)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
    at Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj)
    at Sitecore.Pipelines.Processor.Invoke(PipelineArgs args)
    at Sitecore.Nexus.Pipelines.NexusPipelineApi.Resume(PipelineArgs args, Pipeline pipeline)
    at Sitecore.Pipelines.Pipeline.Resume()
    at Sitecore.Pipelines.Pipeline.DoStart(PipelineArgs args)
    at Sitecore.Pipelines.Pipeline.Start(PipelineArgs args, Boolean atomic)
    at Sitecore.Pipelines.Pipeline.Start(PipelineArgs args)
    at Sitecore.Shell.Applications.FlashUpload.Advanced.UploadTarget.HandleUpload()
    at Sitecore.Shell.Applications.FlashUpload.Advanced.UploadTarget.OnLoad(EventArgs e)
    at System.Web.UI.Control.LoadRecursive()
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    ** Nested Exception **
    Exception: System.NullReferenceException
    Message: Object reference not set to an instance of an object.
    Source: Sitecore.CustomExtensions
    at Sitecore.CustomExtensions.StreamingMediaUploader.Process(UploadArgs args) in C:\...\Sitecore.CustomExtensions\StreamingMediaUploader.cs:line 33

【问题讨论】:

  • 能否提供异常的完整堆栈跟踪?
  • 您在语句之前的 NullRef 检查几乎不会使循环成功,对吧?它让你从方法中返回......
  • 所以在检查之后,它总是不为 NULL 并且永远不会返回,即这个额外的检查使它总是有价值?
  • @Kirk - 不,它不会退出,它实际上进入了循环。这就是为什么这如此疯狂。所以它检查条件,args.Files 不为空,然后进入循环。否则它会抛出一个空引用。在 args.Files 上。
  • 然后我同意 qbeuek 并怀疑异常源自内部。 args.Files 是如何定义的?请务必发布整个堆栈跟踪。

标签: c# reflection nullreferenceexception httpfilecollection


【解决方案1】:

只是猜测,但 CMS 中可能存在一些意外行为(可能是错误)。不管有没有错误,您与 CMS 的合同没有完全定义。

上传文件匹配的原因是您的方法可能被多次调用,最终调用引发异常。

鉴于您正在使用不受您控制的应用程序,您的解决方案是正确的 - 您必须在使用之前检查所提供的所有内容。

【讨论】:

  • 我对此持怀疑态度,因为 args 只是 HttpFileCollection 的包装,它是一个通用的 .NET 构造。我不完全相信这是由于 CMS 造成的异常
  • @Greg R:每次调用您的方法时,您都应该进行一些登录。我敢打赌它不止一次(最后一个需要空检查)。
  • 我设置了一个断点,我正在附加一个调试器,它只被调用一次
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
相关资源
最近更新 更多