Class MessageHeaderCollection
- Namespace
- Silverback.Messaging.Messages
- Assembly
- Silverback.Integration.dll
A modifiable collection of message headers.
public class MessageHeaderCollection : IReadOnlyList<MessageHeader>, IReadOnlyCollection<MessageHeader>, IEnumerable<MessageHeader>, IEnumerable
- Inheritance
-
MessageHeaderCollection
- Implements
- Inherited Members
- Extension Methods
Constructors
MessageHeaderCollection(IReadOnlyCollection<MessageHeader>)
Initializes a new instance of the MessageHeaderCollection class.
public MessageHeaderCollection(IReadOnlyCollection<MessageHeader> headers)
Parameters
headersIReadOnlyCollection<MessageHeader>The headers to be added to the collection.
MessageHeaderCollection(int)
Initializes a new instance of the MessageHeaderCollection class.
public MessageHeaderCollection(int capacity = 0)
Parameters
Properties
Count
public int Count { get; }
Property Value
this[int]
Gets the header at the specified index in the collection.
public MessageHeader this[int index] { get; set; }
Parameters
indexintThe index in the collection.
Property Value
this[string]
Gets or sets the value of the header with the specified name.
public string? this[string name] { get; set; }
Parameters
namestringThe header name.
Property Value
Methods
Add(MessageHeader)
Adds a new header.
public void Add(MessageHeader header)
Parameters
headerMessageHeaderThe header to be added.
Add(string, object?)
Adds a new header.
public void Add(string name, object? value)
Parameters
Add(string, string?)
Adds a new header.
public void Add(string name, string? value)
Parameters
AddIfNotExists(string, object?)
Adds a new header if no header with the same name is already set.
public void AddIfNotExists(string name, object? newValue)
Parameters
AddIfNotExists(string, string?)
Adds a new header if no header with the same name is already set.
public void AddIfNotExists(string name, string? newValue)
Parameters
AddOrReplace(string, object?)
Adds a new header or replaces the header with the same name.
public void AddOrReplace(string name, object? newValue)
Parameters
AddOrReplace(string, string?)
Adds a new header or replaces the header with the same name.
public void AddOrReplace(string name, string? newValue)
Parameters
AddRange(IEnumerable<MessageHeader>)
Adds the new headers.
public void AddRange(IEnumerable<MessageHeader> headers)
Parameters
headersIEnumerable<MessageHeader>The headers to be added.
Contains(string)
Returns a boolean value indicating whether an header with the specified name has already been added to the collection.
public bool Contains(string name)
Parameters
namestringThe name to be checked.
Returns
- bool
A boolean value indicating whether the name was found in the existing headers.
GetEnumerator()
public IEnumerator<MessageHeader> GetEnumerator()
Returns
GetValue(string, bool)
Returns the value of the header with the specified name.
It will return null if no header with that name is found in the collection.
public string? GetValue(string name, bool throwIfNotFound = false)
Parameters
namestringThe name of the header to be retrieved.
throwIfNotFoundboolA 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
nullif not found.
GetValue(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 object? GetValue(string name, Type targetType, bool throwIfNotFound = false)
Parameters
namestringThe name of the header to be retrieved.
targetTypeTypeThe type to convert the header value to.
throwIfNotFoundboolA 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
nullif not found.
GetValueOrDefault(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 object? GetValueOrDefault(string name, Type targetType)
Parameters
namestringThe name of the header to be retrieved.
targetTypeTypeThe type to convert the header value to.
Returns
- object
The header value converted to the target type, or
nullif not found.
GetValueOrDefault<T>(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 T GetValueOrDefault<T>(string name) where T : struct
Parameters
namestringThe name of the header to be retrieved.
Returns
- T
The header value converted to the target type, or
nullif not found.
Type Parameters
TThe type to convert the header value to.
GetValue<T>(string, bool)
Returns the value of the header with the specified name, casting it to the specified type T.
It will return null if no header with that name is found in the collection.
public T? GetValue<T>(string name, bool throwIfNotFound = false) where T : struct
Parameters
namestringThe name of the header to be retrieved.
throwIfNotFoundboolA 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
nullif not found.
Type Parameters
TThe type to convert the header value to.
Remove(MessageHeader)
Removes the specified header.
public void Remove(MessageHeader header)
Parameters
headerMessageHeaderThe header to remove.
Remove(string)
Removes all headers with the specified name.
public void Remove(string name)
Parameters
namestringThe header name.
Replace(string, object?)
Replaces the specified header value.
public int Replace(string name, object? newValue)
Parameters
Returns
- int
The number of headers that have been replaced.
Replace(string, string?)
Replaces the specified header value.
public int Replace(string name, string? newValue)
Parameters
Returns
- int
The number of headers that have been replaced.
TryGetValue(string, out string?)
Checks whether a header with the specified name exists and returns its value.
public bool TryGetValue(string name, out string? value)
Parameters
Returns
- bool
A value indicating whether the header was found.