Version.Parse 方法

定義

多載

Parse(ReadOnlySpan<Char>)

轉換代表相當於 Version 物件版本號碼的指定字元唯讀延伸。

Parse(String)

將版本號碼的字串表示轉換為對等的 Version 物件。

Parse(ReadOnlySpan<Char>)

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> -> Version
Public 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]]

其中 majorminorbuildrevision 是版本號碼四個元件的字串標記法:主要版本號碼、次要版本號碼、組建編號和修訂編號。 選擇性元件會顯示在方括弧 ([ 和 ]) 。 元件必須以指定的順序出現,而且必須以句號分隔。

適用於

Parse(String)

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 -> Version
Public Shared Function Parse (input As String) As Version

參數

input
String

字串,包含要轉換的版本號碼。

傳回

物件,相當於 input 參數中指定的版本號碼。

例外狀況

inputnull

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]]

其中 majorminorbuildrevision 是版本號碼四個元件的字串標記法:主要版本號碼、次要版本號碼、組建編號和修訂編號。 選擇性元件會顯示在方括弧 ([ 和 ]) 。 元件必須以指定的順序出現,而且必須以句號分隔。

重要

因為版本號碼的字串表示必須符合已辨識的模式,所以應用程式在呼叫 Parse 方法來剖析使用者輸入時,應該一律使用例外狀況處理。 或者,您可以呼叫 TryParse 方法來剖析版本號碼的字串表示,並傳回值,指出剖析作業是否成功。

方法是 Parse 方便的方法,相當於呼叫建 Version(String) 構函式。

另請參閱

適用於