Class ConvertibleConverterDictionary
- Namespace
- Cuemon
- Assembly
- Cuemon.Kernel.dll
Represents a collection of converters that map IConvertible implementations to byte arrays.
public class ConvertibleConverterDictionary : IReadOnlyDictionary<Type, Func<IConvertible, byte[]>>, IReadOnlyCollection<KeyValuePair<Type, Func<IConvertible, byte[]>>>, IEnumerable<KeyValuePair<Type, Func<IConvertible, byte[]>>>, IEnumerable
- Inheritance
-
ConvertibleConverterDictionary
- Implements
Examples
The following example demonstrates how to use to register and use type-specific converters that transform
using System;
using System.Linq;
using System.Text;
using Cuemon;
namespace MyApp.Examples;
public class ConvertibleConverterDictionaryExample
{
public void Demonstrate()
{
// Create a dictionary of converters for different IConvertible types
var converters = new ConvertibleConverterDictionary()
.Add<int>(value => BitConverter.GetBytes(value))
.Add<long>(value => BitConverter.GetBytes(value))
.Add<string>(value => Encoding.UTF8.GetBytes(value))
.Add<bool>(value => BitConverter.GetBytes(value));
Console.WriteLine(converters.Count); // 4
// Check if a converter exists for a specific type
Console.WriteLine(converters.ContainsKey(typeof(int))); // True
Console.WriteLine(converters.ContainsKey(typeof(float))); // False
// Use a registered converter
if (converters.TryGetValue(typeof(string), out var stringConverter))
{
byte[] bytes = stringConverter("Hello");
Console.WriteLine(bytes.Length); // 5
Console.WriteLine(Encoding.UTF8.GetString(bytes)); // Hello
// Use the indexer to get a converter
var intConverter = converters[typeof(int)];
if (intConverter != null)
{
byte[] intBytes = intConverter(42);
Console.WriteLine(BitConverter.ToInt32(intBytes)); // 42
// Iterate all registered converters
foreach (var kvp in converters)
{
Console.WriteLine($"{kvp.Key.Name} -> converter registered");
// Output:
// Int32 -> converter registered
// Int64 -> converter registered
// String -> converter registered
// Boolean -> converter registered
// Add a converter using the non-generic Add method
converters.Add(typeof(double), value => BitConverter.GetBytes(value.ToDouble(null)));
Console.WriteLine(converters.Count); // 5
Console.WriteLine(converters.ContainsKey(typeof(double))); // True
}}}}
}
Properties
Count
Gets the number of converters contained in this instance.
public int Count { get; }
Property Value
- int
The number of registered converters.
this[Type]
Gets the converter associated with the specified type.
public Func<IConvertible, byte[]> this[Type type] { get; }
Parameters
typeTypeThe type whose associated converter to retrieve.
Property Value
- Func<IConvertible, byte[]>
The converter associated with
type, or null if no converter is registered for the specified type.
Keys
Gets the collection of types for which converters are registered.
public IEnumerable<Type> Keys { get; }
Property Value
- IEnumerable<Type>
A collection that contains the registered converter types.
Values
Gets the collection of registered converters.
public IEnumerable<Func<IConvertible, byte[]>> Values { get; }
Property Value
- IEnumerable<Func<IConvertible, byte[]>>
A collection that contains the registered converters.
Methods
Add(Type, Func<IConvertible, byte[]>)
Adds a converter for the specified type.
public ConvertibleConverterDictionary Add(Type type, Func<IConvertible, byte[]> converter)
Parameters
typeTypeThe type that implements IConvertible.
converterFunc<IConvertible, byte[]>The delegate that converts an IConvertible instance to a byte array.
Returns
- ConvertibleConverterDictionary
This instance so that additional converters can be configured.
Exceptions
- ArgumentNullException
typeis null.- ArgumentOutOfRangeException
typedoes not implement IConvertible.
Add<T>(Func<T, byte[]>)
Adds a converter for the specified T.
public ConvertibleConverterDictionary Add<T>(Func<T, byte[]> converter) where T : IConvertible
Parameters
Returns
- ConvertibleConverterDictionary
This instance so that additional converters can be configured.
Type Parameters
TThe type that implements IConvertible.
Exceptions
- TypeArgumentOutOfRangeException
Tdoes not implement IConvertible.
ContainsKey(Type)
Determines whether this dictionary contains a converter for the specified key.
public bool ContainsKey(Type key)
Parameters
keyTypeThe type to locate.
Returns
GetEnumerator()
Returns an enumerator that iterates through the registered converters.
public IEnumerator<KeyValuePair<Type, Func<IConvertible, byte[]>>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<Type, Func<IConvertible, byte[]>>>
An enumerator that can be used to iterate through the registered converters.
TryGetValue(Type, out Func<IConvertible, byte[]>)
Gets the converter associated with the specified key.
public bool TryGetValue(Type key, out Func<IConvertible, byte[]> value)
Parameters
keyTypeThe type whose associated converter to retrieve.
valueFunc<IConvertible, byte[]>When this method returns, contains the converter associated with the specified
key, if the key is found; otherwise, the default value for the type of thevalueparameter.
Returns
Exceptions
- ArgumentNullException
keyis null.