ArgumentNullException/PlatformNotSupportedException 仅在调试器中来自 mscorlib

ArgumentNullException/PlatformNotSupportedException from mscorlib only in debugger

我在调试 (VS 2015) 时看到了一对异常,这些异常是在单击文本框时发生的。它们并不总是发生,但它们似乎总是在相同的情况下发生(我找不到可以解释它的模式)。它们似乎也只发生一次——如果我继续,忽略它们,我不会看到它们再次发生。它们似乎也与特定的文本框无关。我的代码是现有代码和附加代码的混合体,同样的问题出现在我没有接触过的代码中。

我尝试创建一个非常简单的项目,在 window 中只有一个文本框,但我没有看到那里的问题(但同样,它不会在所有情况下都会发生)。

我无法在 try-catch 中捕获它们 -- 调试器会在它们被抛出时检测到它们,但看起来 Microsoft 正在内部捕获它们而不是抛出它们的代码。如果我在调试器外部尝试 运行ning,我的 try-catch 什么也抓不到,代码也没有明显的问题。

第一个是 mscorlib.dll 抛出的 ArgumentNullException。如果我在抛出该异常后继续,PresentationCore.dll 将抛出 PlatformNotSupportedException。如果我在那之后继续,我的代码似乎 运行 没问题。

我将在下面提供详细信息,但我想知道,因为它们似乎是在 Microsoft 的代码内部处理的(并且似乎与我的代码无关),我是否应该假设这是我从未打算看到的东西?如果不是这种情况,任何人都可以建议这里可能发生的事情或我如何调试它吗?

我的平台是 Windows 7,我正在使用 "Any CPU" 构建,当前目标框架是 .Net 4.5.2(但我尝试使用高达 4.6.1 的框架,没有明显的改变)。

初始异常:

System.ArgumentNullException occurred
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: type
  ParamName=type
  Source=mscorlib
  StackTrace:
       at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.GetActivationFactory(Type type)
  InnerException: 

抛出时调用堆栈的一部分:

mscorlib.dll!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.GetActivationFactory(System.Type type) Line 1279   C#
    PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.GetWinRtActivationFactory(bool forceInitialization) Unknown
    PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.InputPane() Unknown
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.GetForWindow(System.Windows.Interop.HwndSource source = {System.Windows.Interop.HwndSource})    Unknown
    PresentationCore.dll!MS.Internal.Interop.TipTsfHelper.Show(System.Windows.DependencyObject focusedObject)   Unknown
    PresentationCore.dll!System.Windows.UIElement.Focus()   Unknown
    PresentationFramework.dll!System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(System.Windows.Documents.TextEditor This = {System.Windows.Documents.TextEditor}) Unknown
    PresentationFramework.dll!System.Windows.Documents.TextEditorMouse.OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e = {System.Windows.Input.MouseButtonEventArgs})    Unknown
    PresentationFramework.dll!System.Windows.Controls.Primitives.TextBoxBase.OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)   Unknown
    PresentationCore.dll!System.Windows.UIElement.OnMouseDownThunk(object sender = {System.Windows.Controls.TextBox}, System.Windows.Input.MouseButtonEventArgs e = {System.Windows.Input.MouseButtonEventArgs})    Unknown
    PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) Unknown
    PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target)   Unknown
    PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown
    PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {System.Windows.Controls.Grid}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}, bool reRaised = false) Unknown
    PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.Grid}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs})    Unknown
    PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs})  Unknown
    PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args, bool trusted) Unknown
    PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() Unknown
    PresentationCore.dll!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs input)  Unknown
    PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport)   Unknown
    PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(System.IntPtr hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel)   Unknown
    PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(System.IntPtr hwnd = {System.IntPtr}, MS.Internal.Interop.WindowMessage msg = WM_LBUTTONDOWN, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false)    Unknown
    PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false)    Unknown
    WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false) Unknown
    WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)  Unknown
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source = {System.Windows.Threading.Dispatcher}, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler = null)  Unknown
    WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs)   Unknown
    WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr})  Unknown
    [Native to Managed Transition]  

抛出异常(由于空类型参数)的 mscorlib 代码是这样的:

        [SecurityCritical]
        public static IActivationFactory GetActivationFactory(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            if (type.IsWindowsRuntimeObject && type.IsImport)
            {
                return (IActivationFactory)Marshal.GetNativeActivationFactory(type);
            }
            else
            {
#if FEATURE_COMINTEROP_WINRT_MANAGED_ACTIVATION
                return GetManagedActivationFactory(type);
#else 
                // Managed factories are not supported so as to minimize public surface (and test effort)
                throw new NotSupportedException();
#endif
            }
        }

如果我继续超出此异常,它会抛出以下内容:

  System.PlatformNotSupportedException occurred
  HResult=-2146233031
  Message=Operation is not supported on this platform.
  Source=PresentationCore
  StackTrace:
       at MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane..ctor(Nullable`1 hwnd)
  InnerException: 

此时的部分调用堆栈:

PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.InputPane(System.IntPtr? hwnd)  Unknown
    PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.GetForWindow(System.Windows.Interop.HwndSource source)  Unknown
    PresentationCore.dll!MS.Internal.Interop.TipTsfHelper.Show(System.Windows.DependencyObject focusedObject)   Unknown
    PresentationCore.dll!System.Windows.UIElement.Focus()   Unknown
    PresentationFramework.dll!System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(System.Windows.Documents.TextEditor This = {System.Windows.Documents.TextEditor}) Unknown
    PresentationFramework.dll!System.Windows.Documents.TextEditorMouse.OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e = {System.Windows.Input.MouseButtonEventArgs})    Unknown
    PresentationFramework.dll!System.Windows.Controls.Primitives.TextBoxBase.OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)   Unknown
    PresentationCore.dll!System.Windows.UIElement.OnMouseDownThunk(object sender = {System.Windows.Controls.TextBox}, System.Windows.Input.MouseButtonEventArgs e = {System.Windows.Input.MouseButtonEventArgs})    Unknown
    PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) Unknown
    PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target)   Unknown
    PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown
    PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {System.Windows.Controls.Grid}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}, bool reRaised = false) Unknown
    PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.Grid}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs})    Unknown
    PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs})  Unknown
    PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args, bool trusted) Unknown
    PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() Unknown
    PresentationCore.dll!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs input)  Unknown
    PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport)   Unknown
    PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(System.IntPtr hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel)   Unknown
    PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(System.IntPtr hwnd = {System.IntPtr}, MS.Internal.Interop.WindowMessage msg = WM_LBUTTONDOWN, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false)    Unknown
    PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false)    Unknown
    WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false) Unknown
    WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)  Unknown
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source = {System.Windows.Threading.Dispatcher}, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler = null)  Unknown
    WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs)   Unknown
    WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr})  Unknown
    [Native to Managed Transition]  
    [Managed to Native Transition]

从那里开始,一切都运行符合预期。

升级到 Visual Studio 2017 后,此问题已消失。