Move generic Get/Set into Interface

This commit is contained in:
Ben Adams 2015-12-31 20:18:24 +00:00
parent 364a712cb3
commit 62ec29b6af
4 changed files with 34 additions and 30 deletions

View File

@ -93,6 +93,16 @@ namespace Microsoft.AspNet.Http.Features
}
}
public TFeature Get<TFeature>()
{
return (TFeature)this[typeof(TFeature)];
}
public void Set<TFeature>(TFeature instance)
{
this[typeof(TFeature)] = instance;
}
private class KeyComparer : IEqualityComparer<KeyValuePair<Type, object>>
{
public bool Equals(KeyValuePair<Type, object> x, KeyValuePair<Type, object> y)

View File

@ -1,30 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNet.Http.Features
{
public static class FeatureCollectionExtensions
{
/// <summary>
/// Retrieves the requested feature from the collection.
/// </summary>
/// <typeparam name="TFeature">The feature key.</typeparam>
/// <param name="features">The collection.</param>
/// <returns>The requested feature, or null if it is not present.</returns>
public static TFeature Get<TFeature>(this IFeatureCollection features)
{
return (TFeature)features[typeof(TFeature)];
}
/// <summary>
/// Sets the given feature in the collection.
/// </summary>
/// <typeparam name="TFeature">The feature key.</typeparam>
/// <param name="features">The collection.</param>
/// <param name="instance">The feature value.</param>
public static void Set<TFeature>(this IFeatureCollection features, TFeature instance)
{
features[typeof(TFeature)] = instance;
}
}
}

View File

@ -27,5 +27,19 @@ namespace Microsoft.AspNet.Http.Features
/// <param name="key"></param>
/// <returns>The requested feature, or null if it is not present.</returns>
object this[Type key] { get; set; }
/// <summary>
/// Retrieves the requested feature from the collection.
/// </summary>
/// <typeparam name="TFeature">The feature key.</typeparam>
/// <returns>The requested feature, or null if it is not present.</returns>
TFeature Get<TFeature>();
/// <summary>
/// Sets the given feature in the collection.
/// </summary>
/// <typeparam name="TFeature">The feature key.</typeparam>
/// <param name="instance">The feature value.</param>
void Set<TFeature>(TFeature instance);
}
}

View File

@ -358,6 +358,16 @@ namespace Microsoft.AspNet.Owin
throw new NotSupportedException();
}
public TFeature Get<TFeature>()
{
return (TFeature)this[typeof(TFeature)];
}
public void Set<TFeature>(TFeature instance)
{
this[typeof(TFeature)] = instance;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();