简体   繁体   English

system.version超过3个小数点C#

[英]system.version more than 3 decimal points c#

I am currently receiving the following error - "Version string portion was too short or too long" 我目前收到以下错误-“版本字符串部分太短或太长”

When using this statement - 使用此语句时-

records = records.OrderBy(r => new Version(r.RefNo)).ToList();

To order a list of string's called RefNo. 订购一个称为RefNo的字符串列表。 It fails on 25.1.2.1.2 so i assume it is because it has four decimal points? 它在25.1.2.1.2上失败,所以我认为是因为它有四个小数点? as it works ok with 3.... 因为它在3 ....下可以正常工作

Is the max four deciaml points for system.version? 是system.version的最大四个deciaml点?

Thanks 谢谢

A Version can only have 4 parts: 一个Version只能包含4个部分:

major, minor, build, and revision, in that order, and all separated by periods. 主要,次要,构建和修订,以该顺序排列,并全部用句点分隔。

That's why your approach fails. 这就是您的方法失败的原因。 You could create an extension-method which handles this case, fe: 您可以创建一个扩展方法来处理这种情况,例如:

public static Version TryParseVersion(this string versionString)
{
    if (string.IsNullOrEmpty(versionString))
        return null;

    String[] tokens = versionString.Split('.');
    if (tokens.Length < 2 || !tokens.All(t => t.All(char.IsDigit)))
        return null;

    if (tokens.Length > 4)
    {
        int maxVersionLength = tokens.Skip(4).Max(t => t.Length);
        string normalizedRest = string.Concat(tokens.Skip(4).Select(t => t.PadLeft(maxVersionLength, '0')));
        tokens[3] = $"{tokens[3].PadLeft(maxVersionLength, '0')}{normalizedRest}";
        Array.Resize(ref tokens, 4);
    }

    versionString = string.Join(".", tokens);
    bool valid = Version.TryParse(versionString, out Version v);
    return valid ? v : null;
}

Now you can use it in this way: 现在,您可以通过以下方式使用它:

records = records
   .OrderBy(r => r.RefNo.TryParseVersion())
   .ToList();

With your sample this new version string will be parsed(successfully): 25.1.2.12 使用您的示例,新版本字符串将被解析(成功): 25.1.2.12

See MSDN 参见MSDN

Costructor public Version(string version) 构造函数public Version(string version)

A string containing the major, minor, build, and revision numbers, where each number is delimited with a period character ('.'). 包含主要,次要,内部和修订版本号的字符串,其中每个数字都用句点字符('。')分隔。

Makes a total of 4 numbers. 总共使4个数字。

Means the string is limited to 4 numbers, 5 lead to an error. 表示字符串限制为4个数字,其中5个导致错误。

Also the costructors with int 's as parameters only support 1 to 4 parameters. 同样,以int为参数的构造函数仅支持1-4个参数。

sorry for the late reply but here is the finished extension method i used with a couple of alterations - 很抱歉收到您的延迟回复,但这是我完成的扩展方法,并做了一些改动-

public static Version ParseRefNo(this string refNoString)
        {
            if (string.IsNullOrEmpty(refNoString))
                return null;

            String[] tokens = refNoString.Split('.');
            if (tokens.Length < 2 || !tokens.All(t => t.All(char.IsDigit)))
                return null;

            if (tokens.Length > 4)
            {
                int maxVersionLength = tokens.Skip(4).Max(t => t.Length);
                string normalizedRest = string.Concat(tokens.Skip(4).Select(t => t.PadLeft(maxVersionLength, '0')));
                tokens[3] = $"{tokens[3].PadLeft(maxVersionLength, '0')}{normalizedRest}";
                Array.Resize(ref tokens, 4);
            }

            refNoString = string.Join(".", tokens);
            Version v = null;
            bool valid = Version.TryParse(refNoString, out v);
            return valid ? v : null;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM