Class ParseResult<T>
The result of a parse operation.
Since 1.0.x
Availability netstandard2.0
Inheritance
Object
ParseResult<T>
Inherited Members
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
Namespace: NodaTime.Text
Assembly: NodaTime.dll
Syntax
public sealed class ParseResult<T>
Type Parameters
Name | Description |
---|---|
T | The type which was parsed, such as a LocalDateTime. |
Properties
Exception
Gets an exception indicating the cause of the parse failure.
Since 1.2.x
Availability netstandard2.0
Declaration
public Exception Exception { get; }
Property Value
Type | Description |
---|---|
Exception | The exception indicating the cause of the parse failure. |
Remarks
This property is typically used to wrap parse failures in higher level exceptions.
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | The parse operation succeeded. |
Success
Indicates whether the parse operation was successful.
Since 1.0.x
Availability netstandard2.0
Declaration
public bool Success { get; }
Property Value
Type | Description |
---|---|
Boolean | true if the parse operation was successful; otherwise false. |
Remarks
This returns True if and only if fetching the value with the Value property will return with no exception.
Value
Gets the value from the parse operation if it was successful, or throws an exception indicating the parse failure
otherwise.
Since 1.0.x
Availability netstandard2.0
Declaration
public T Value { get; }
Property Value
Type | Description |
---|---|
T | The result of the parsing operation if it was successful. |
Remarks
This method is exactly equivalent to calling the GetValueOrThrow() method, but is terser if the code is
already clear that it will throw if the parse failed.
Methods
Convert<TTarget>(Func<T, TTarget>)
Converts this result to a new target type, either by executing the given projection
for a success result, or propagating the exception provider for failure.
Since 2.1.x
Availability netstandard2.0
Declaration
public ParseResult<TTarget> Convert<TTarget>(Func<T, TTarget> projection)
Parameters
Type | Name | Description |
---|---|---|
Func<T, TTarget> | projection | The projection to apply for the value of this result, if it's a success result. |
Returns
Type | Description |
---|---|
ParseResult<TTarget> | A ParseResult for the target type, either with a value obtained by applying the specified projection to the value in this result, or with the same error as this result. |
Type Parameters
Name | Description |
---|---|
TTarget | The target type of the conversion. |
ConvertError<TTarget>()
Converts this result to a new target type by propagating the exception provider.
This parse result must already be an error result.
Since 2.1.x
Availability netstandard2.0
Declaration
public ParseResult<TTarget> ConvertError<TTarget>()
Returns
Type | Description |
---|---|
ParseResult<TTarget> | A ParseResult for the target type, with the same error as this result. |
Type Parameters
Name | Description |
---|---|
TTarget |
ForException(Func<Exception>)
Produces a ParseResult which represents a failed parsing operation.
Since 2.1.x
Availability netstandard2.0
Declaration
public static ParseResult<T> ForException(Func<Exception> exceptionProvider)
Parameters
Type | Name | Description |
---|---|---|
Func<Exception> | exceptionProvider | A delegate that produces the exception representing the error that caused the parse to fail. |
Returns
Type | Description |
---|---|
ParseResult<T> | A ParseResult representing a failed parsing operation. |
Remarks
This method accepts a delegate rather than the exception itself, as creating an
exception can be relatively slow: if the client doesn't need the actual exception, just the information
that the parse failed, there's no point in creating the exception.
ForValue(T)
Produces a ParseResult which represents a successful parse operation.
Since 2.1.x
Availability netstandard2.0
Declaration
public static ParseResult<T> ForValue(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The successfully parsed value. |
Returns
Type | Description |
---|---|
ParseResult<T> | A ParseResult representing a successful parsing operation. |
Remarks
When T is a reference type,
value
should not be null,
but this isn't currently checked.GetValueOrThrow()
Gets the value from the parse operation if it was successful, or throws an exception indicating the parse failure
otherwise.
Since 1.0.x
Availability netstandard2.0
Declaration
public T GetValueOrThrow()
Returns
Type | Description |
---|---|
T | The result of the parsing operation if it was successful. |
Remarks
This method is exactly equivalent to fetching the Value property, but more explicit in terms of throwing
an exception on failure.
TryGetValue(T, out T)
Returns the success value, and sets the out parameter to either
the specified failure value of T or the successful parse result value.
Since 1.0.x
Availability netstandard2.0
Declaration
public bool TryGetValue(T failureValue, out T result)
Parameters
Type | Name | Description |
---|---|---|
T | failureValue | The "default" value to set in result if parsing failed. |
T | result | The parameter to store the parsed value in on success. |
Returns
Type | Description |
---|---|
Boolean | True if this parse result was successful, or false otherwise. |