Skip to content

VersionConverter.ConvertTo/From may fail with Version and InstanceDescriptor types #58331

Closed

Description

@koszeggy

Description

When I accidentally used the System.ComponentModel.VersionConverter class in my unit tests instead of my VersionConverter it turned out that the System implementation may fail in some cases.

Reproduction

  • Call VersionConverter.ConvertTo with a Version instance that has only two or three version number elements and use destinationType: typeof(Version). The instantiation will fail because Version(int,int,int,int) constructor does not accept -1 values.
  • Call VersionConverter.ConvertFrom with an InstanceDescriptor returned by the VersionConverter.ConvertTo method from a similar Version instance.

Suggested fix

  • To convert to a Version use Version.Clone instead
  • To create an InstanceDescriptor use the string constructor or the Parse method instead.

Other information

I looked at the runtime unit tests and it seems that Version and InstanceDescriptor target types are not tested for actual conversions but only for the CanConvertTo/CanConvertFrom methods.

Activity

  1. ghost added
    untriaged
    on Aug 29, 2021
  2. added this to the 7.0.0 milestone on Sep 2, 2021
  3. removed
    untriaged
    on Sep 2, 2021
  4. modified the milestones: 7.0.0, Future on Jan 14, 2022
  5. dotnet-policy-service commented on Oct 19, 2026

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  6. dotnet-policy-service commented on Oct 19, 2026

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  7. added
    backlog-cleanup-candidate
    on Oct 19, 2026
  8. koszeggy commented on Oct 23, 2026

    @koszeggy
    ContributorAuthor

    Just noting that the issue still persists in .NET 11 RC 1. As I have my own VersionConverter, I can live without it, but the fix would be very simple:

    Converting to Version (and a similar fix in ConvertFrom as well):

    if (destinationType == typeof(Version))
    {
    -    return new Version(version.Major, version.Minor, version.Build, version.Revision);
    +    return version.Clone(); // to avoid ArgumentOutOfRangeException if Build or Revision is -1
    }

    Converting to InstanceDescriptor (the ConvertFrom already handles it in the base class):

    if (destinationType == typeof(InstanceDescriptor))
    {
    -    ConstructorInfo? ctor = typeof(Version).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }, null);
    -    Debug.Assert(ctor != null, "Couldn't find constructor");
    -    return new InstanceDescriptor(ctor, new object[] { version.Major, version.Minor, version.Build, version.Revision });
    +    MethodInfo parseMethod = typeof(Version).GetMethod(nameof(Version.Parse), [typeof(string)])!;
    +    Debug.Assert(parseMethod != null, "Couldn't find Version.Parse");
    +    return new InstanceDescriptor(parseMethod, new[] { version.ToString() });
    }
  9. added
    in-pr
    and removed
    backlog-cleanup-candidate
    on Oct 23, 2026
  10. added a commit that references this issue on Oct 24, 2026
    f0c7a08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.ComponentModelin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions