using System;
namespace EmbedIO
{
partial class HttpContextExtensions
{
/// Gets the item associated with the specified key.
/// The desired type of the item.
/// The on which this method is called.
/// The key whose value to get from the Items dictionary.
///
/// When this method returns, the item associated with the specified key,
/// if the key is found in Items
/// and the associated value is of type ;
/// otherwise, the default value for .
/// This parameter is passed uninitialized.
///
/// if the item is found and is of type ;
/// otherwise, .
/// is .
/// is .
public static bool TryGetItem(this IHttpContext @this, object key, out T value)
{
if (@this.Items.TryGetValue(key, out var item) && item is T typedItem)
{
value = typedItem;
return true;
}
#pragma warning disable CS8653 // value is non-nullable - We are returning false, so value is undefined.
value = default;
#pragma warning restore CS8653
return false;
}
/// Gets the item associated with the specified key.
/// The desired type of the item.
/// The on which this method is called.
/// The key whose value to get from the Items dictionary.
/// The item associated with the specified key,
/// if the key is found in Items
/// and the associated value is of type ;
/// otherwise, the default value for .
public static T GetItem(this IHttpContext @this, object key)
=> @this.Items.TryGetValue(key, out var item) && item is T typedItem ? typedItem : default;
}
}