You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.InvalidCastException when using custom data type converter & running tests with "dotnet test" #6713
I'm getting System.InvalidCastException error when running my tests with library version 3.11.0 or higher using "dotnet test" command. The error does not appear in older versions or when runnign tests with Visual Studio built-in test runner.
The error:
System.InvalidCastException: [A]ExampleDataType cannot be cast to [B]ExampleDataType. Type A originates from 'TestProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'TestProjectPath\bin\Release\net48\TestProjectName.dll'. Type B originates from 'TestProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'ResultsDirectoryFolder\DeployFolder\Out\TestProjectName.dll'
This happens when using custom TypeConverter and using TypeDescriptor.GetConverter for accessing that TypeConverter - and then using static type reference to cast object in type converter.
Steps To Reproduce
Add the following test method and helper classes to a test project test class. Then run "dotnet test pathToTestProjectCsproj"
[TestMethod]publicvoidExample(){objectvalue=newExampleDataType();varconverter=TypeDescriptor.GetConverter(typeof(ExampleDataType));converter.ConvertTo(null,null,value,null);// throws the error}[TypeConverter(typeof(ExampleDataTypeTypeConverter))]publicclassExampleDataType{}publicclassExampleDataTypeTypeConverter:TypeConverter{publicoverrideboolCanConvertFrom(ITypeDescriptorContextcontext,TypesourceType)=>true;publicoverrideboolCanConvertTo(ITypeDescriptorContextcontext,TypedestinationType)=>true;publicoverrideobjectConvertFrom(ITypeDescriptorContextcontext,CultureInfoculture,objectvalue)=>newExampleDataType();publicoverrideobjectConvertTo(ITypeDescriptorContextcontext,CultureInfoculture,objectvalue,TypedestinationType)=>(ExampleDataType)value;}
Expected behavior
No error happens, type is casted as usual (like before version 3.11.0)
I had to dig a bit deeper as it seems that my MVP example didn't work as standalone solution to reproduce. After removing stuff from my test project (can't send the original as it is company code), I managed to filter out the missing requirement for the error to appear: DeploymentItem-usage somewhere in the code. Here is the mvp example I managed to reproduce by that finding. Note that even though the DeploymentItem is attached to the offender class in the example, it doesn't have to. It is enough for some other testclass in the project to have a deployment item.
TLDR: There also has to be a DeploymentItem used in some TestClass in the offending test project.
Issue described in #6676 still exists with 4.0.1.
On top of that now AppDomain.CurrentDomain.BaseDirectory does not point to the out folder anymore and hence even more tests fail.
Still only with vstest.console.exe, works in visual studio 2022 and 2026 insider.
Last working version: 3.9.3
Unfortunately I can't, it's a huge ~15+ year old project that has been migrated to the latest studio versions over the years, maybe it has something to do with that although it's still strange that it only happens under the described circumstances.
Until now I wasn't able to reproduce it in newly created sample projects/solutions.
AppDomain.CurrentDomain.FriendlyName
In Visual Studio (works): TestSourceHost: Enumerating source (path to deploy\out\Test.dll)
With vstest.console.exe (doesn't work): Test Runner
I gave /inisolation a try with 4.0.1 and this time it worked, with 3.11.0 it didn't make a difference.
Is this the supposed solution?
It also changed AppDomain.CurrentDomain.FriendlyName to match how it looks when running in Visual Studio: TestSourceHost: Enumerating source (path to deploy\out\Test.dll)
And lastly, -- RunConfiguration.DisableAppDomain=false also helped with 4.0.1 and I would imagine this is the way to go?
Epicycle23 I think the issue here is now well understood. My recommendation is to try -- RunConfiguration.DisableAppDomain=true (yes, true). I suspect if you explicitly specify any value (true or false), it will work. But it fails if you don't specify it at all. But disabling app domain (i.e, passing true) is much better for performance reasons.
Jakub Jareš (@nohwnd) I think what happens here is that our check for running under testhost so that we force enable appdomain is failing when not running in isolation, because we are running directly under vstest.console.
I tested the fix in 4.0.2 and now tests pass for both example project and real project when using "dotnet test" and Visual studio bult-in test runner.
However I'm getting the original error now when running the tests using Resharper test runner in Visual Studio (both the newest available versions). I'm not sure if this is an issue with the library or Resharper though. I'll also contact resharper team about the issue.
Activity
Youssef1313 commentedon Oct 10, 2025
Probably related to #6676 as well.
FYI Jakub Jareš (@nohwnd)
Youssef1313 commentedon Oct 10, 2025
jon-erik-klint Can you please provide a complete zipped repro/solution that shows the issue?
jon-erik-klint commentedon Oct 10, 2025
I had to dig a bit deeper as it seems that my MVP example didn't work as standalone solution to reproduce. After removing stuff from my test project (can't send the original as it is company code), I managed to filter out the missing requirement for the error to appear: DeploymentItem-usage somewhere in the code. Here is the mvp example I managed to reproduce by that finding. Note that even though the DeploymentItem is attached to the offender class in the example, it doesn't have to. It is enough for some other testclass in the project to have a deployment item.
TLDR: There also has to be a DeploymentItem used in some TestClass in the offending test project.
ExampleProject.zip
Youssef1313 commentedon Oct 10, 2025
Thank you jon-erik-klint for working on getting a repro. We will investigate this.
Youssef1313 commentedon Oct 14, 2025
jon-erik-klint We just released 4.0.1 with a fix for this issue. Can you try that please? Thanks!
nohwnd commentedon Oct 15, 2025
Epicycle23 Your issue was marked as dupe. Fix for the bug was released in 4.0.1, please check it out.
awierda commentedon Oct 15, 2025
I had the same problem with MSTest 3.1.1 and MSTest 4.0.0:
Updating MSTest to 4.0.1 fixed the problem.
Thanks!
Epicycle23 commentedon Oct 17, 2025
Issue described in #6676 still exists with 4.0.1.
On top of that now AppDomain.CurrentDomain.BaseDirectory does not point to the out folder anymore and hence even more tests fail.
Still only with vstest.console.exe, works in visual studio 2022 and 2026 insider.
Last working version: 3.9.3
Youssef1313 commentedon Oct 17, 2025
Epicycle23 Can you please upload a repro?
Epicycle23 commentedon Oct 17, 2025
Unfortunately I can't, it's a huge ~15+ year old project that has been migrated to the latest studio versions over the years, maybe it has something to do with that although it's still strange that it only happens under the described circumstances.
Until now I wasn't able to reproduce it in newly created sample projects/solutions.
Youssef1313 commentedon Oct 17, 2025
Epicycle23 Are you using VSTest or Microsoft.Testing.Platform?
Inside your test method, if you evaluate
AppDomain.CurrentDomain.FriendlyName, what do you get?Epicycle23 commentedon Oct 20, 2025
Microsoft.Testing.Platform 2.0.1
MSTest.TestFramework 4.0.1
AppDomain.CurrentDomain.FriendlyName
In Visual Studio (works): TestSourceHost: Enumerating source (path to deploy\out\Test.dll)
With vstest.console.exe (doesn't work): Test Runner
Epicycle23 commentedon Oct 21, 2025
I gave /inisolation a try with 4.0.1 and this time it worked, with 3.11.0 it didn't make a difference.
Is this the supposed solution?
It also changed AppDomain.CurrentDomain.FriendlyName to match how it looks when running in Visual Studio: TestSourceHost: Enumerating source (path to deploy\out\Test.dll)
And lastly, -- RunConfiguration.DisableAppDomain=false also helped with 4.0.1 and I would imagine this is the way to go?
Youssef1313 commentedon Nov 6, 2025
Epicycle23 I think the issue here is now well understood. My recommendation is to try
-- RunConfiguration.DisableAppDomain=true(yes, true). I suspect if you explicitly specify any value (true or false), it will work. But it fails if you don't specify it at all. But disabling app domain (i.e, passing true) is much better for performance reasons.Jakub Jareš (@nohwnd) I think what happens here is that our check for running under
testhostso that we force enable appdomain is failing when not running in isolation, because we are running directly under vstest.console.Youssef1313 commentedon Nov 6, 2025
Tracking this in #6894
jon-erik-klint commentedon Nov 18, 2025
I tested the fix in 4.0.2 and now tests pass for both example project and real project when using "dotnet test" and Visual studio bult-in test runner.
However I'm getting the original error now when running the tests using Resharper test runner in Visual Studio (both the newest available versions). I'm not sure if this is an issue with the library or Resharper though. I'll also contact resharper team about the issue.
Youssef1313 commentedon Nov 18, 2025
jon-erik-klint Thanks for confirming this is fixed in 4.0.2.
Regarding ReSharper, this needs to be reported to them.
jon-erik-klint commentedon Nov 19, 2025
Updating here if someone else is experiencing the issue on resharper side. There is now an issue being tracked on their side that you can vote on for priority: https://youtrack.jetbrains.com/issue/RSRP-502313.