Číst v angličtině

Sdílet prostřednictvím


Version.Parse Metoda

Definice

Přetížení

Parse(ReadOnlySpan<Byte>)
Parse(ReadOnlySpan<Char>)

Převede zadaný rozsah znaků jen pro čtení, který představuje číslo verze, na ekvivalentní Version objekt.

Parse(String)

Převede řetězcovou reprezentaci čísla verze na ekvivalentní Version objekt.

Parse(ReadOnlySpan<Byte>)

public:
 static Version ^ Parse(ReadOnlySpan<System::Byte> utf8Text);
public static Version Parse(ReadOnlySpan<byte> utf8Text);
static member Parse : ReadOnlySpan<byte> -> Version
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte)) As Version

Parametry

utf8Text
ReadOnlySpan<Byte>

Návraty

Platí pro

.NET 10
Produkt Verze
.NET 10

Parse(ReadOnlySpan<Char>)

Zdroj:
Version.cs
Zdroj:
Version.cs
Zdroj:
Version.cs

Převede zadaný rozsah znaků jen pro čtení, který představuje číslo verze, na ekvivalentní Version objekt.

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

Parametry

input
ReadOnlySpan<Char>

Rozsah znaků jen pro čtení, který obsahuje číslo verze, která se má převést.

Návraty

Objekt, který odpovídá číslu verze zadanému v parametru input .

Výjimky

input má méně než dvě nebo více než čtyři komponenty verze.

Alespoň jedna komponenta v input souboru je menší než nula.

Alespoň jedna komponenta v input souboru není celé číslo.

Alespoň jedna komponenta v input představuje číslo, které je větší než Int32.MaxValue.

Poznámky

Parametr input musí mít následující formát:

major.minor[.build[.revision]]

kde major, minor, builda revision jsou řetězcové vyjádření čtyř součástí čísla verze: číslo hlavní verze, číslo podverze, číslo buildu a číslo revize. Volitelné komponenty se zobrazují v hranatých závorkách ([ a ]). Součásti musí být uvedeny v zadaném pořadí a musí být oddělené tečkami.

Platí pro

.NET 10 a další verze
Produkt Verze
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

Parse(String)

Zdroj:
Version.cs
Zdroj:
Version.cs
Zdroj:
Version.cs

Převede řetězcovou reprezentaci čísla verze na ekvivalentní Version objekt.

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

Parametry

input
String

Řetězec, který obsahuje číslo verze k převodu.

Návraty

Objekt, který odpovídá číslu verze zadanému v parametru input .

Výjimky

input je null.

input má méně než dvě nebo více než čtyři komponenty verze.

Alespoň jedna komponenta v input souboru je menší než nula.

Alespoň jedna komponenta v input souboru není celé číslo.

Alespoň jedna komponenta v input představuje číslo, které je větší než Int32.MaxValue.

Příklady

Následující příklad používá metodu Parse k analýze počtu řetězců, které obsahují informace o verzi.

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'.

Poznámky

Parametr input musí mít následující formát:

major.minor[.build[.revision]]

kde major, minor, builda revision jsou řetězcové vyjádření čtyř součástí čísla verze: číslo hlavní verze, číslo podverze, číslo buildu a číslo revize. Volitelné komponenty se zobrazují v hranatých závorkách ([ a ]). Součásti musí být uvedeny v zadaném pořadí a musí být oddělené tečkami.

Důležité

Vzhledem k tomu, že řetězcové vyjádření čísla verze musí odpovídat rozpoznaného vzoru, měly by aplikace vždy používat zpracování výjimek při volání Parse metody k parsování vstupu uživatele. Alternativně můžete volat metodu TryParse , která parsuje řetězcovou reprezentaci čísla verze a vrátí hodnotu, která označuje, zda byla operace parsování úspěšná.

Metoda Parse je pohodlná metoda; je ekvivalentní volání konstruktoru Version(String) .

Viz také

Platí pro

.NET 10 a další verze
Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0