Version.Parse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Parse(ReadOnlySpan<Byte>) |
將指定只讀的 UTF-8 字元範圍(代表版本號)轉換為等效的版本物件。 |
| Parse(ReadOnlySpan<Char>) |
將代表版本號的指定唯讀字元範圍轉換為等效 Version 物件。 |
| Parse(String) |
將版本號的字串表示轉換為等價 Version 物件。 |
Parse(ReadOnlySpan<Byte>)
- 來源:
- Version.cs
- 來源:
- Version.cs
將指定只讀的 UTF-8 字元範圍(代表版本號)轉換為等效的版本物件。
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
參數
- utf8Text
- ReadOnlySpan<Byte>
一個只讀的 UTF-8 字元區段,包含可轉換的版本號。
傳回
一個等價於參數中 utf8Text 指定的版本號的物件。
例外狀況
utf8Text 版本組件少於兩個或超過四個。
至少有一個分量 在 中 utf8Text 小於零。
至少 utf8Text 有一個分量不是整數。
中 utf8Text 至少有一個分量代表一個大於 MaxValue的數字。
適用於
Parse(ReadOnlySpan<Char>)
- 來源:
- Version.cs
- 來源:
- Version.cs
- 來源:
- Version.cs
- 來源:
- Version.cs
- 來源:
- 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]]
其中 major、 minor、 build、 和 revision 分別是版本號四個組成部分的字串表示:主要版本號、次要版本號、建置號和修訂號。 可選元件以方括號標示([ 和 ])。 各組成部分必須依照指定順序出現,且必須以週期分隔。
適用於
Parse(String)
- 來源:
- Version.cs
- 來源:
- Version.cs
- 來源:
- Version.cs
- 來源:
- Version.cs
- 來源:
- 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 指定的版本號的物件。
例外狀況
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 分別是版本號四個組成部分的字串表示:主要版本號、次要版本號、建置號和修訂號。 可選元件以方括號標示([ 和 ])。 各組成部分必須依照指定順序出現,且必須以週期分隔。
Important
由於版本號的字串表示必須符合可識別的模式,應用程式在呼叫 Parse 方法解析使用者輸入時,應始終使用例外處理。 或者,你也可以呼叫 TryParse 解析版本號的字串表示法,並回傳一個值,表示解析操作是否成功。
此Parse方法是一種便利方法;它等同於呼叫建構子。Version(String)