NUnitCallContext
This class ensures the CallContext is correctly cleared
or restored around framework actions. This is important if running multiple assemblies within the same
process, to ensure no leakage from one assembly to the next. See https://github.com/nunit/nunit-console/issues/325
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Messaging;
namespace NUnit.Framework.Internal
{
internal class NUnitCallContext : IDisposable
{
[System.Runtime.CompilerServices.Nullable(1)]
private readonly object _oldContext;
[System.Runtime.CompilerServices.Nullable(1)]
public const string TestExecutionContextKey = "NUnit.Framework.TestExecutionContext";
public NUnitCallContext()
{
_oldContext = CallContext.GetData("NUnit.Framework.TestExecutionContext");
}
public void Dispose()
{
if (_oldContext == null)
CallContext.FreeNamedDataSlot("NUnit.Framework.TestExecutionContext");
else
CallContext.SetData("NUnit.Framework.TestExecutionContext", _oldContext);
}
}
}