Table of Contents

Class MessageHeaderEnumerableExtensions

Namespace
Silverback.Messaging.Messages
Assembly
Silverback.Integration.dll

Add some helper methods to the IEnumerable<T> of MessageHeader.

public static class MessageHeaderEnumerableExtensions
Inheritance
MessageHeaderEnumerableExtensions
Inherited Members

Methods

Contains(IEnumerable<MessageHeader>, string)

Returns a boolean value indicating whether an header with the specified name has already been added to the collection.

public static bool Contains(this IEnumerable<MessageHeader> headers, string name)

Parameters

headers IEnumerable<MessageHeader>

The enumerable containing the headers to be searched.

name string

The name to be checked.

Returns

bool

A boolean value indicating whether the name was found in the existing headers.

GetValue(IEnumerable<MessageHeader>, string, bool)

Returns the value of the header with the specified name.

By default, it will return null if no header with that name is found in the collection but this behavior can be changed setting the throwIfNotFound parameter to true.

public static string? GetValue(this IEnumerable<MessageHeader> headers, string name, bool throwIfNotFound = false)

Parameters

headers IEnumerable<MessageHeader>

The enumerable containing the headers to be searched.

name string

The name of the header to be retrieved.

throwIfNotFound bool

A boolean value specifying whether an exception must be thrown if the header with the specified name is not found (or the value cannot be converted to the specified type).

Returns

string

The header value converted to the target type, or null if not found.

GetValue(IEnumerable<MessageHeader>, string, Type, bool)

Returns the value of the header with the specified name, casting it to the specified type.

By default, it will return null if no header with that name is found in the collection but this behavior can be changed setting the throwIfNotFound parameter to true.

public static object? GetValue(this IEnumerable<MessageHeader> headers, string name, Type targetType, bool throwIfNotFound = false)

Parameters

headers IEnumerable<MessageHeader>

The enumerable containing the headers to be searched.

name string

The name of the header to be retrieved.

targetType Type

The type to convert the header value to.

throwIfNotFound bool

A boolean value specifying whether an exception must be thrown if the header with the specified name is not found (or the value cannot be converted to the specified type).

Returns

object

The header value converted to the target type, or null if not found.

GetValueOrDefault(IEnumerable<MessageHeader>, string, Type)

Returns the value of the header with the specified name, casting it to the specified type.

It will return the default value for the target type if no header with that name is found in the collection.

public static object? GetValueOrDefault(this IEnumerable<MessageHeader> headers, string name, Type targetType)

Parameters

headers IEnumerable<MessageHeader>

The enumerable containing the headers to be searched.

name string

The name of the header to be retrieved.

targetType Type

The type to convert the header value to.

Returns

object

The header value converted to the target type, or null if not found.

GetValueOrDefault<T>(IEnumerable<MessageHeader>, string)

Returns the value of the header with the specified name, casting it to the specified type T.

It will return the default value for the type T if no header with that name is found in the collection.

public static T GetValueOrDefault<T>(this IEnumerable<MessageHeader> headers, string name) where T : struct

Parameters

headers IEnumerable<MessageHeader>

The enumerable containing the headers to be searched.

name string

The name of the header to be retrieved.

Returns

T

The header value converted to the target type, or null if not found.

Type Parameters

T

The type to convert the header value to.

GetValue<T>(IEnumerable<MessageHeader>, string, bool)

Returns the value of the header with the specified name, casting it to the specified type T.

By default, it will return null if no header with that name is found in the collection but this behavior can be changed setting the throwIfNotFound parameter to true.

public static T? GetValue<T>(this IEnumerable<MessageHeader> headers, string name, bool throwIfNotFound = false) where T : struct

Parameters

headers IEnumerable<MessageHeader>

The enumerable containing the headers to be searched.

name string

The name of the header to be retrieved.

throwIfNotFound bool

A boolean value specifying whether an exception must be thrown if the header with the specified name is not found (or the value cannot be converted to the specified type).

Returns

T?

The header value converted to the target type, or null if not found.

Type Parameters

T

The type to convert the header value to.

TryGetValue(IEnumerable<MessageHeader>, string, out string?)

Checks whether an header with the specified name exists and returns its value.

public static bool TryGetValue(this IEnumerable<MessageHeader> headers, string name, out string? value)

Parameters

headers IEnumerable<MessageHeader>

The enumerable containing the headers to be searched.

name string

The name of the header to be retrieved.

value string

The header value.

Returns

bool

A value indicating whether the header was found.