Class HttpContentDeserializerCollection
- Namespace
- Kampute.HttpClient
- Assembly
- Kampute.HttpClient.dll
Represents a specialized collection of IHttpContentDeserializer instances.
public sealed class HttpContentDeserializerCollection : ICollection<IHttpContentDeserializer>, IReadOnlyCollection<IHttpContentDeserializer>, IEnumerable<IHttpContentDeserializer>, IEnumerable
- Inheritance
-
HttpContentDeserializerCollection
- Implements
- Inherited Members
Remarks
This collection provides capabilities for managing IHttpContentDeserializer instances, including adding, removing, and selecting deserializers based on media types and model types. It leverages internal caches to optimize performance for frequently accessed deserializers, significantly enhancing efficiency in scenarios where media types and model types are repeatedly queried.
Constructors
HttpContentDeserializerCollection()
Initializes a new instance of the HttpContentDeserializerCollection class.
public HttpContentDeserializerCollection()
Properties
Count
Gets the number of IHttpContentDeserializer instances contained in the collection.
public int Count { get; }
Property Value
- int
The number of IHttpContentDeserializer instances contained in the collection.
Methods
Add(IHttpContentDeserializer)
Adds an IHttpContentDeserializer to the collection if an instance of the same type doesn't already exist.
public void Add(IHttpContentDeserializer deserializer)
Parameters
deserializer
IHttpContentDeserializerThe IHttpContentDeserializer to add.
Exceptions
- ArgumentNullException
Thrown if
deserializer
is null.- ArgumentException
Thrown if an instance of the same type already exists in the collection.
Clear()
Removes all items from the collection.
public void Clear()
Contains(IHttpContentDeserializer)
Determines whether the collection contains a specific IHttpContentDeserializer.
public bool Contains(IHttpContentDeserializer deserializer)
Parameters
deserializer
IHttpContentDeserializerThe IHttpContentDeserializer to locate in the collection.
Returns
Find<T>()
Finds an IHttpContentDeserializer by its type.
public T Find<T>() where T : IHttpContentDeserializer
Returns
- T
The instance of IHttpContentDeserializer of the specified type, or null if not found.
Type Parameters
T
The type of the deserializer to find.
GetAcceptableMediaTypes(Type?)
Retrieves all supported media types for a specified model type from the collection of deserializers.
public IEnumerable<string> GetAcceptableMediaTypes(Type? modelType)
Parameters
modelType
TypeThe type of the model for which to retrieve supported media types.
Returns
- IEnumerable<string>
An enumerable of strings that represent the media types supported for deserializing the specified model type.
Remarks
This method determines the supported media types for deserializing content based on the given model type.
If the model type is null, this method returns a collection containing only "*/*"
, signifying that all media types are acceptable.
For non-null model types, the method aggregates media types supported by the registered deserializers for that specific model type.
GetAcceptableMediaTypes(Type?, Type?)
Retrieves all supported media types for a specified model type and error type from the collection of deserializers.
public IEnumerable<string> GetAcceptableMediaTypes(Type? modelType, Type? errorType)
Parameters
modelType
TypeThe type of the model for which to retrieve supported media types.
errorType
TypeThe type of the error for which to retrieve supported media types.
Returns
- IEnumerable<string>
An enumerable of strings representing the supported media types for the specified types.
Remarks
This method determines the supported media types for deserializing content based on the given modelType
and errorType
.
If both modelType
and errorType
are provided, it aggregates and returns the media types that support deserializing either type.
If only modelType
is provided and errorType
is null, the result includes media types exclusively supporting the modelType
.
Conversely, if modelType
is null and errorType
is provided, the result includes media types supporting the errorType
,
augmented by "*/*"
to indicate that all media types are acceptable for the modelType
.
If both parameters are null, the method defaults to returning "*/*"
only, implying general acceptability of any media type.
GetDeserializerFor(string, Type)
Retrieves the first IHttpContentDeserializer instances in the collection that support deserializing a specific media type and model type.
public IHttpContentDeserializer? GetDeserializerFor(string mediaType, Type modelType)
Parameters
mediaType
stringThe media type to deserialize.
modelType
TypeThe type of the model to deserialize.
Returns
- IHttpContentDeserializer
An instance of IHttpContentDeserializer that can deserialize the specified media type and model type, or null if none is found.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<IHttpContentDeserializer> GetEnumerator()
Returns
Remove(IHttpContentDeserializer)
Removes the first occurrence of a specific IHttpContentDeserializer from the collection.
public bool Remove(IHttpContentDeserializer deserializer)
Parameters
deserializer
IHttpContentDeserializerThe IHttpContentDeserializer to remove from the collection.