@@ -39,11 +39,21 @@ namespace Lucene.Net.Support.ExceptionHandling
3939 public abstract class ExceptionScanningTestCase : AssemblyScanningTestCase
4040 {
4141 // Internal types references
42- private static readonly Type DebugAssertExceptionType =
42+ private static readonly Type ClrDebugAssertExceptionType =
4343 // .NET 5/.NET Core 3.x
4444 Type . GetType ( "System.Diagnostics.DebugProvider+DebugAssertException, System.Private.CoreLib" )
4545 // .NET Core 2.x
4646 ?? Type . GetType ( "System.Diagnostics.Debug+DebugAssertException, System.Private.CoreLib" ) ;
47+
48+ private static readonly Type TestHostDebugAssertExceptionType =
49+ // Microsoft.NET.Test.Sdk 16.6.0+ (used by Visual Studio Test Explorer)
50+ Type . GetType ( "Microsoft.VisualStudio.TestPlatform.TestHost.DebugAssertException, testhost" ) ;
51+
52+ private static readonly Type DebugAssertExceptionType =
53+ // Microsoft.NET.Test.Sdk 16.6.0+ (used by Visual Studio Test Explorer)
54+ TestHostDebugAssertExceptionType
55+ // .NET Core
56+ ?? ClrDebugAssertExceptionType ;
4757 // .NET Framework doesn't throw in this case
4858
4959 private static readonly Type MetadataExceptionType =
@@ -465,15 +475,28 @@ private static IDictionary<Type, Func<Type, string, object>> LoadNonStandardExce
465475 } ;
466476
467477 // Special case - this doesn't exist on .NET Framework, so we only add it if not null
468- if ( DebugAssertExceptionType is not null )
478+ if ( ClrDebugAssertExceptionType is not null )
469479 {
470- result [ DebugAssertExceptionType ] = ( exceptionType , message ) =>
480+ result [ ClrDebugAssertExceptionType ] = ( exceptionType , message ) =>
471481 {
472482 //private sealed class DebugAssertException : Exception
473483 //{
474484 // internal DebugAssertException(string? message, string? detailMessage, string? stackTrace)
475485 BindingFlags flags = BindingFlags . NonPublic | BindingFlags . Instance ;
476- return Activator . CreateInstance ( DebugAssertExceptionType , flags , null , new object [ ] { message , null , null } , CultureInfo . InvariantCulture ) ;
486+ return Activator . CreateInstance ( ClrDebugAssertExceptionType , flags , null , new object [ ] { message , null , null } , CultureInfo . InvariantCulture ) ;
487+ } ;
488+ }
489+
490+ // Special case - Microsoft.NET.Test.Sdk 16.6.0+ contains its own Debug.Assert type that is thrown when debugging tests
491+ if ( TestHostDebugAssertExceptionType is not null )
492+ {
493+ result [ TestHostDebugAssertExceptionType ] = ( exceptionType , message ) =>
494+ {
495+ //internal sealed class DebugAssertException : Exception
496+ //{
497+ //public DebugAssertException(string message, string stackTrace) : base(message)
498+ BindingFlags flags = BindingFlags . NonPublic | BindingFlags . Instance ;
499+ return Activator . CreateInstance ( TestHostDebugAssertExceptionType , flags , null , new object [ ] { message , string . Empty } , CultureInfo . InvariantCulture ) ;
477500 } ;
478501 }
479502
0 commit comments