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
VersionConverter.ConvertTo/From may fail with Version and InstanceDescriptor types #58331
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.
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.
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.
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):
Activity
dotnet-policy-service commentedon Oct 19, 2026
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.
dotnet-policy-service commentedon Oct 19, 2026
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.
koszeggy commentedon Oct 23, 2026
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 inConvertFromas 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(theConvertFromalready 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() }); }Fix VersionConverter for versions without Build/Revision (#136423)