// handles not only the simple cast, but also value type widening, etc. public static T Convert <T>(object source) { // first check the common cases if (source is T) { return((T)source); } if (source == null) { if (typeof(T).IsValueType && !IsNullableType(typeof(T))) { throw Fx.Exception.AsError(new InvalidCastException(SRCore.CannotConvertObject(source, typeof(T)))); } return(default(T)); } T result; if (TryNumericConversion <T>(source, out result)) { return(result); } throw Fx.Exception.AsError(new InvalidCastException(SRCore.CannotConvertObject(source, typeof(T)))); }
public static Exception AssertAndFailFast(string description) { Fx.Assert(description); string failFastMessage = SRCore.FailFastMessage(description); // The catch is here to force the finally to run, as finallys don't run until the stack walk gets to a catch. // The catch makes sure that the finally will run before the stack-walk leaves the frame, but the code inside is impossible to reach. try { try { Fx.Exception.TraceFailFast(failFastMessage); } finally { Environment.FailFast(failFastMessage); } } catch { throw; } return(null); // we'll never get here since we've just fail-fasted }
public static void ThrowIfNonPositiveArgument(TimeSpan timeout, string argumentName) { if (timeout <= TimeSpan.Zero) { throw Fx.Exception.ArgumentOutOfRange(argumentName, timeout, SRCore.TimeoutMustBePositive(argumentName, timeout)); } }
public T Dequeue(TimeSpan timeout) { T value; if (!this.Dequeue(timeout, out value)) { throw Fx.Exception.AsError(new TimeoutException(SRCore.TimeoutInputQueueDequeue(timeout))); } return(value); }
protected override void Invoke() { this.callback(this.state, this.TimedOut ? new TimeoutException(SRCore.TimeoutOnOperation(this.originalTimeout)) : null); }
public ArgumentException ArgumentNullOrEmpty(string paramName) { return(this.Argument(paramName, SRCore.ArgumentNullOrEmpty(paramName))); }
public AssertionFailedException(string description) : base(SRCore.ShipAssertExceptionMessage(description)) { }
protected virtual Exception CreateQuotaExceededException(int maxSizeQuota) { return(new InvalidOperationException(SRCore.BufferedOutputStreamQuotaExceeded(maxSizeQuota))); }
static void ThrowInvalidAsyncResult(IAsyncResult result) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.InvalidAsyncResultImplementation(result.GetType()))); }
protected void Complete(bool didCompleteSynchronously) { if (this.isCompleted) { throw Fx.Exception.AsError(new InvalidOperationException(SRCore.AsyncResultCompletedTwice(this.GetType()))); } #if DEBUG this.marker.AsyncResult = null; this.marker = null; if (!Fx.FastDebug && completeStack == null) { this.completeStack = new StackTrace(); } #endif this.completedSynchronously = didCompleteSynchronously; if (this.OnCompleting != null) { // Allow exception replacement, like a catch/throw pattern. try { this.OnCompleting(this, this.exception); } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } this.exception = e; } } if (didCompleteSynchronously) { // If we completedSynchronously, then there's no chance that the manualResetEvent was created so // we don't need to worry about a race Fx.Assert(this.manualResetEvent == null, "No ManualResetEvent should be created for a synchronous AsyncResult."); this.isCompleted = true; } else { lock (this.ThisLock) { this.isCompleted = true; if (this.manualResetEvent != null) { this.manualResetEvent.Set(); } } } if (this.Callback != null) { try { if (this.VirtualCallback != null) { this.VirtualCallback(this.Callback, this); } else { this.Callback(this); } } #pragma warning disable 1634 #pragma warning suppress 56500 // transferring exception to another thread catch (Exception e) { if (Fx.IsFatal(e)) { throw; } throw Fx.Exception.AsError(new CallbackException(SRCore.AsyncCallbackThrewException, e)); } #pragma warning restore 1634 } }