Skip to content

System.InvalidCastException when using custom data type converter & running tests with "dotnet test" #6713

Closed

Description

@jon-erik-klint

The bug

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]
public void Example()
{
    object value = new ExampleDataType();
    var converter = TypeDescriptor.GetConverter(typeof(ExampleDataType));
    converter.ConvertTo(null, null, value, null); // throws the error
}

[TypeConverter(typeof(ExampleDataTypeTypeConverter))]
public class ExampleDataType
{
}

public class ExampleDataTypeTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => true;

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) => true;

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) => new ExampleDataType();

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) => (ExampleDataType)value;
}

Expected behavior

No error happens, type is casted as usual (like before version 3.11.0)

Actual behavior

The mentioned exception is thrown.

Activity

  1. Youssef1313 commented on Oct 10, 2025

    @Youssef1313
    Member

    Probably related to #6676 as well.

    FYI Jakub Jareš (@nohwnd)

  2. Youssef1313 commented on Oct 10, 2025

    @Youssef1313
    Member

    jon-erik-klint Can you please provide a complete zipped repro/solution that shows the issue?

  3. added
    needs/info
    state/cannot-reproduce
    and removed
    needs/triage
    on Oct 10, 2025
  4. jon-erik-klint commented on Oct 10, 2025

    @jon-erik-klint
    Author

    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

  5. added
    type/regression
    area/mstest
    and removed
    needs/info
    state/cannot-reproduce
    on Oct 10, 2025
  6. Youssef1313 commented on Oct 10, 2025

    @Youssef1313
    Member

    Thank you jon-erik-klint for working on getting a repro. We will investigate this.

  7. Youssef1313 commented on Oct 14, 2025

    @Youssef1313
    Member

    jon-erik-klint We just released 4.0.1 with a fix for this issue. Can you try that please? Thanks!

  8. nohwnd commented on Oct 15, 2025

    @nohwnd
    Member

    Epicycle23 Your issue was marked as dupe. Fix for the bug was released in 4.0.1, please check it out.

  9. awierda commented on Oct 15, 2025

    @awierda

    I had the same problem with MSTest 3.1.1 and MSTest 4.0.0:

    • Running in Visual Studio Test Explorer succeeded.
    • Running on the build server (probably with "dotnet test ...") failed.
    • Some tests have a DeploymentItem.

    Updating MSTest to 4.0.1 fixed the problem.
    Thanks!

  10. Epicycle23 commented on Oct 17, 2025

    @Epicycle23

    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

  11. Youssef1313 commented on Oct 17, 2025

    @Youssef1313
    Member

    Epicycle23 Can you please upload a repro?

  12. Epicycle23 commented on Oct 17, 2025

    @Epicycle23

    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.

  13. Youssef1313 commented on Oct 17, 2025

    @Youssef1313
    Member

    Epicycle23 Are you using VSTest or Microsoft.Testing.Platform?

    Inside your test method, if you evaluate AppDomain.CurrentDomain.FriendlyName, what do you get?

  14. Epicycle23 commented on Oct 20, 2025

    @Epicycle23

    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

  15. Epicycle23 commented on Oct 21, 2025

    @Epicycle23

    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?

  16. Youssef1313 commented on Nov 6, 2025

    @Youssef1313
    Member

    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.

  17. Youssef1313 commented on Nov 6, 2025

    @Youssef1313
    Member

    Tracking this in #6894

  18. jon-erik-klint commented on Nov 18, 2025

    @jon-erik-klint
    Author

    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.

  19. Youssef1313 commented on Nov 18, 2025

    @Youssef1313
    Member

    jon-erik-klint Thanks for confirming this is fixed in 4.0.2.
    Regarding ReSharper, this needs to be reported to them.

  20. jon-erik-klint commented on Nov 19, 2025

    @jon-erik-klint
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/deployment-itemarea/mstesttype/regression

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions