Version.Parse 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| Parse(ReadOnlySpan<Byte>) | |
| Parse(ReadOnlySpan<Char>) | 将表示版本号的指定字符只读范围转换为等效的 Version 对象。 | 
| Parse(String) | 将版本号的字符串表示形式转换为等效的 Version 对象。 | 
Parse(ReadOnlySpan<Byte>)
- Source:
- Version.cs
public:
 static Version ^ Parse(ReadOnlySpan<System::Byte> utf8Text);public static Version Parse(ReadOnlySpan<byte> utf8Text);static member Parse : ReadOnlySpan<byte> -> VersionPublic Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte)) As Version参数
- utf8Text
- ReadOnlySpan<Byte>
返回
适用于
Parse(ReadOnlySpan<Char>)
- Source:
- Version.cs
- Source:
- Version.cs
- Source:
- Version.cs
- Source:
- Version.cs
将表示版本号的指定字符只读范围转换为等效的 Version 对象。
public:
 static Version ^ Parse(ReadOnlySpan<char> input);public static Version Parse(ReadOnlySpan<char> input);static member Parse : ReadOnlySpan<char> -> VersionPublic Shared Function Parse (input As ReadOnlySpan(Of Char)) As Version参数
- input
- ReadOnlySpan<Char>
字符的只读范围,其中包含要转换的版本号。
返回
一个等效于 input 参数中指定的版本号的对象。
例外
              input 少于两个组件或多于四个版本组件。
在 input 中至少有一个组件小于零。
在 input 中至少有一个组件不是整数。
中的 input 至少一个分量表示大于 Int32.MaxValue 的数字。
注解
参数 input 必须具有以下格式:
major.minor[.build[.revision]]
其中 major、 minor、 build和 revision 是版本号的四个组件的字符串表示形式:主版本号、次要版本号、内部版本号和修订号。 可选组件显示在方括号中, ([ 和 ]) 。 组件必须按指定顺序显示,并且必须用句点分隔。
适用于
Parse(String)
- Source:
- Version.cs
- Source:
- Version.cs
- Source:
- Version.cs
- Source:
- Version.cs
将版本号的字符串表示形式转换为等效的 Version 对象。
public:
 static Version ^ Parse(System::String ^ input);public static Version Parse(string input);static member Parse : string -> VersionPublic Shared Function Parse (input As String) As Version参数
- input
- String
包含要转换的版本号的字符串。
返回
一个等效于 input 参数中指定的版本号的对象。
例外
              input 为 null。
              input 少于两个组件或多于四个版本组件。
在 input 中至少有一个组件小于零。
在 input 中至少有一个组件不是整数。
中的 input 至少一个分量表示大于 Int32.MaxValue 的数字。
示例
以下示例使用 Parse 方法分析包含版本信息的多个字符串。
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'.
open System
let parseVersion (input: string) =
    try
        let ver = Version.Parse input
        printfn $"Converted '{input} to {ver}."
    with
    | :? ArgumentNullException ->
        printfn "Error: String to be parsed is null."
    | :? ArgumentOutOfRangeException ->
        printfn $"Error: Negative value in '{input}'."
    | :? ArgumentException ->
        printfn $"Error: Bad number of components in '{input}'."
    | :? FormatException ->
        printfn $"Error: Non-integer value in '{input}'."
    | :? OverflowException ->
        printfn $"Error: Number out of range in '{input}'."                  
[<EntryPoint>]
let main _ =
    let input = "4.0"
    parseVersion input
    
    let input = "4.0."
    parseVersion input
    
    let input = "1.1.2"
    parseVersion input
    
    let input = "1.1.2.01702"
    parseVersion input
    
    let input = "1.1.2.0702.119"
    parseVersion input
    
    let input =  "1.3.5.2150000000"
    parseVersion input
    0
// 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 必须具有以下格式:
major.minor[.build[.revision]]
其中 major、 minor、 build和 revision 是版本号的四个组件的字符串表示形式:主版本号、次要版本号、内部版本号和修订号。 可选组件显示在方括号中, ([ 和 ]) 。 组件必须按指定顺序显示,并且必须用句点分隔。
重要
由于版本号的字符串表示形式必须符合已识别的模式,因此在调用 Parse 方法分析用户输入时,应用程序应始终使用异常处理。 或者,可以调用 TryParse 方法来分析版本号的字符串表示形式,并返回一个值,该值指示分析操作是否成功。
方法 Parse 是一种便捷方法;它等效于调用 Version(String) 构造函数。