// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.ComponentModel; using System.Web.Http; namespace System.Net.Http { [EditorBrowsable(EditorBrowsableState.Never)] public static class HttpResponseMessageExtensions { /// /// Attempts to retrieve a strongly-typed value from a . /// /// /// If is an instance of /// attempts to retrieve the if it is compatible with . /// If it is it returns true and sets . If not it returns false and /// sets to the default instance of . /// /// The type of the value to retrieve. /// The response. /// Will contain the retrieved value if this method succeeds. /// Returns true if the response has a content with a value that can be cast to , /// false otherwise. public static bool TryGetContentValue(this HttpResponseMessage response, out T value) { if (response == null) { throw Error.ArgumentNull("response"); } ObjectContent content = response.Content as ObjectContent; if (content != null) { if (content.Value is T) { value = (T)content.Value; return true; } } value = default(T); return false; } /// /// Attaches the given to the if the response does not already /// have a pointer to a request. /// /// The response. /// The request. internal static void EnsureResponseHasRequest(this HttpResponseMessage response, HttpRequestMessage request) { if (response != null && response.RequestMessage == null) { response.RequestMessage = request; } } } }