Version.Parse 方法

定义

重载

Parse(ReadOnlySpan<Char>)

将表示版本号的指定字符只读范围转换为等效的 Version 对象。Converts the specified read-only span of characters that represents a version number to an equivalent Version object.

Parse(String)

将版本号的字符串表示形式转换为等效的 Version 对象。Converts the string representation of a version number to an equivalent Version object.

Parse(ReadOnlySpan<Char>)

将表示版本号的指定字符只读范围转换为等效的 Version 对象。Converts the specified read-only span of characters that represents a version number to an equivalent Version object.

public:
 static Version ^ Parse(ReadOnlySpan<char> input);
public static Version Parse (ReadOnlySpan<char> input);
static member Parse : ReadOnlySpan<char> -> Version
Public Shared Function Parse (input As ReadOnlySpan(Of Char)) As Version

参数

input
ReadOnlySpan<Char>

字符的只读范围,其中包含要转换的版本号。A read-only span of characters that contains a version number to convert.

返回

一个等效于 input 参数中指定的版本号的对象。An object that is equivalent to the version number specified in the input parameter.

异常

input 少于两个组件或多于四个版本组件。input has fewer than two or more than four version components.

input 中至少有一个组件小于零。At least one component in input is less than zero.

input 中至少有一个组件不是整数。At least one component in input is not an integer.

input 中至少有一个组件表示大于 MaxValue 的数。At least one component in input represents a number that is greater than MaxValue.

注解

input 参数必须采用以下格式:The input parameter must have the following format:

major.minor[.build[.revision]]

其中 majorminorbuildrevision 是版本号的四个组件的字符串表示形式:分别是主版本号、次版本号、内部版本号和修订号。where major, minor, build, and revision are the string representations of the version number's four components: major version number, minor version number, build number, and revision number, respectively. 可选组件显示在方括号([和])中。Optional components are shown in square brackets ([ and ]). 组件必须按指定顺序显示,并且必须以句点分隔。The components must appear in the specified order and must be separated by periods.

Parse(String)

将版本号的字符串表示形式转换为等效的 Version 对象。Converts the string representation of a version number to an equivalent Version object.

public:
 static Version ^ Parse(System::String ^ input);
public static Version Parse (string input);
static member Parse : string -> Version
Public Shared Function Parse (input As String) As Version

参数

input
String

包含要转换的版本号的字符串。A string that contains a version number to convert.

返回

一个等效于 input 参数中指定的版本号的对象。An object that is equivalent to the version number specified in the input parameter.

异常

inputnullinput is null.

input 少于两个组件或多于四个版本组件。input has fewer than two or more than four version components.

input 中至少有一个组件小于零。At least one component in input is less than zero.

input 中至少有一个组件不是整数。At least one component in input is not an integer.

input 中至少有一个组件表示大于 MaxValue 的数。At least one component in input represents a number that is greater than MaxValue.

示例

下面的示例使用 Parse 方法分析多个包含版本信息的字符串。The following example uses the Parse method to parse a number of strings that contain version information.

using System;

public class Example
{
   public static void Main()
   {
      string input = "4.0";
      ParseVersion(input);
      
      input = "4.0.";
      ParseVersion(input);
      
      input = "1.1.2";
      ParseVersion(input);
      
      input = "1.1.2.01702";
      ParseVersion(input);
      
      input = "1.1.2.0702.119";
      ParseVersion(input);
      
      input =  "1.3.5.2150000000";
      ParseVersion(input);
   }
   
   private static void ParseVersion(string input)
   {
      try {
         Version ver = Version.Parse(input);
         Console.WriteLine("Converted '{0} to {1}.", input, ver);
      }
      catch (ArgumentNullException) {
         Console.WriteLine("Error: String to be parsed is null.");
      }
      catch (ArgumentOutOfRangeException) {
         Console.WriteLine("Error: Negative value in '{0}'.", input);
      }
      catch (ArgumentException) {
         Console.WriteLine("Error: Bad number of components in '{0}'.", 
                           input);
      }
      catch (FormatException) {
         Console.WriteLine("Error: Non-integer value in '{0}'.", input);
      }
      catch (OverflowException) {   
         Console.WriteLine("Error: Number out of range in '{0}'.", input);                  
      }   
   }
}
// The example displays the following output:
//       Converted '4.0 to 4.0.
//       Error: Non-integer value in '4.0.'.
//       Converted '1.1.2 to 1.1.2.
//       Converted '1.1.2.01702 to 1.1.2.1702.
//       Error: Bad number of components in '1.1.2.0702.119'.
//       Error: Number out of range in '1.3.5.2150000000'.
Module Example
   Public Sub Main()
      Dim input As String = "4.0"
      ParseVersion(input)
      
      input = "4.0."
      ParseVersion(input)
      
      input = "1.1.2"
      ParseVersion(input)
      
      input = "1.1.2.01702"
      ParseVersion(input)
      
      input = "1.1.2.0702.119"
      ParseVersion(input)
      
      input =  "1.3.5.2150000000"
      ParseVersion(input)
   End Sub
   
   Private Sub ParseVersion(input As String)
      Try
         Dim ver As Version = Version.Parse(input)
         Console.WriteLine("Converted '{0} to {1}.", input, ver)
      Catch e As ArgumentNullException
         Console.WriteLine("Error: String to be parsed is null.")
      Catch e As ArgumentOutOfRangeException
         Console.WriteLine("Error: Negative value in '{0}'.", input)
      Catch e As ArgumentException
         Console.WriteLine("Error: Bad number of components in '{0}'.", 
                           input)
      Catch e As FormatException
         Console.WriteLine("Error: Non-integer value in '{0}'.", input)
      Catch e As OverflowException   
         Console.WriteLine("Error: Number out of range in '{0}'.", input)                  
      End Try   
   End Sub
End Module
' The example displays the following output:
'       Converted '4.0 to 4.0.
'       Error: Non-integer value in '4.0.'.
'       Converted '1.1.2 to 1.1.2.
'       Converted '1.1.2.01702 to 1.1.2.1702.
'       Error: Bad number of components in '1.1.2.0702.119'.
'       Error: Number out of range in '1.3.5.2150000000'.

注解

input 参数必须采用以下格式:The input parameter must have the following format:

major.minor[.build[.revision]]

其中 majorminorbuildrevision 是版本号的四个组件的字符串表示形式:分别是主版本号、次版本号、内部版本号和修订号。where major, minor, build, and revision are the string representations of the version number's four components: major version number, minor version number, build number, and revision number, respectively. 可选组件显示在方括号([和])中。Optional components are shown in square brackets ([ and ]). 组件必须按指定顺序显示,并且必须以句点分隔。The components must appear in the specified order and must be separated by periods.

重要

由于版本号的字符串表示形式必须符合可识别的模式,因此当调用 Parse 方法来分析用户输入时,应用程序应始终使用异常处理。Because the string representation of a version number must conform to a recognized pattern, applications should always use exception handling when calling the Parse method to parse user input. 或者,你可以调用 TryParse 方法来分析版本号的字符串表示形式,并返回一个指示分析操作是否成功的值。Alternatively, you can call the TryParse method to parse the string representation of a version number and return a value that indicates whether the parse operation succeeded.

Parse 方法是一种简便的方法;它等效于调用 Version(String) 构造函数。The Parse method is a convenience method; it is equivalent to calling the Version(String) constructor.

另请参阅

适用于