ConvertibleConverterDictionary Class
Definition
- Namespace
- Cuemon
- Assemblies
- 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
| Name | Description |
|---|---|
| Count | Gets the number of converters contained in this instance. |
| this[Type] | Gets the converter associated with the specified |
| Keys | Gets the collection of types for which converters are registered. |
| Values | Gets the collection of registered converters. |
Methods
| Name | Description |
|---|---|
| Add(Type, Func<IConvertible, byte[]>) | Adds a converter for the specified |
| Add<T>(Func<T, byte[]>) | Adds a converter for the specified |
| ContainsKey(Type) | Determines whether this dictionary contains a converter for the specified |
| GetEnumerator() | Returns an enumerator that iterates through the registered converters. |
| TryGetValue(Type, out Func<IConvertible, byte[]>) | Gets the converter associated with the specified |