This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Microsoft.AspNet.Mvc.Core/Formatters/FormatterCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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. | ||
|
||
using System.Collections.ObjectModel; | ||
|
||
namespace Microsoft.AspNet.Mvc.Formatters | ||
{ | ||
/// <summary> | ||
/// Represents a collection of formatters. | ||
/// </summary> | ||
/// <typeparam name="TFormatter">The type of formatters in the collection.</typeparam> | ||
public class FormatterCollection<TFormatter> : Collection<TFormatter> | ||
{ | ||
|
||
/// <summary> | ||
/// Removes all formatters of the specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The type to remove.</typeparam> | ||
public void RemoveType<T>() where T : TFormatter | ||
{ | ||
for (var i = Count - 1; i >= 0; i--) | ||
{ | ||
var formatter = this[i]; | ||
if (formatter is T) | ||
{ | ||
RemoveAt(i); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
test/Microsoft.AspNet.Mvc.Core.Test/Formatters/FormatterCollectionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNet.Mvc.Formatters | ||
{ | ||
public class FormatterCollectionTest | ||
{ | ||
[Fact] | ||
public void RemoveType_RemovesAllOfType() | ||
{ | ||
// Arrange | ||
var collection = new FormatterCollection<IOutputFormatter> | ||
{ | ||
new TestOutputFormatter(), | ||
new AnotherTestOutputFormatter(), | ||
new TestOutputFormatter() | ||
}; | ||
|
||
// Act | ||
collection.RemoveType<TestOutputFormatter>(); | ||
|
||
// Assert | ||
var formatter = Assert.Single(collection); | ||
Assert.IsType(typeof(AnotherTestOutputFormatter), formatter); | ||
} | ||
|
||
private class TestOutputFormatter : OutputFormatter | ||
{ | ||
public override Task WriteResponseBodyAsync(OutputFormatterContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
private class AnotherTestOutputFormatter : OutputFormatter | ||
{ | ||
public override Task WriteResponseBodyAsync(OutputFormatterContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} | ||
} |