EnumConverter
EnumConverter.TryGet(fromType, toType, out writer) tries to get enum converter.
bool ok = EnumConverter.TryGet(typeof(Country), typeof(string), out IWriter converter);
string value = converter.ReadAs<Country, string>(Country.Finland);
EnumConverter.EnumBoxToInteger<E, I> converts enum type to integer type as Enum envelope type (and also object).
IWriterToRef<Enum, ulong> converter = EnumConverter.EnumBoxToInteger<Country, ulong>.Instance;
ulong value = converter.Read(Country.Finland);
[Flags]
public enum Countries
{
Finland = 1,
Sweden = 2,
Norway = 4
}
public enum Country
{
Finland = 1,
Sweden = 2,
Norway = 3
}
EnumConverter.IntegerToEnumBox<E, I> converts integer type to enum type as Enum envelope type (and also object).
IWriterToRef<ulong, Enum> converter = EnumConverter.IntegerToEnumBox<Country, ulong>.Instance;
Enum @enum = converter.Read(1UL);
EnumConverter.EnumValueToInteger<E, I> converts enum value to integer type.
IWriterToRef<Country, ulong> converter = EnumConverter.EnumValueToInteger<Country, ulong>.Instance;
ulong value = converter.Read(Country.Finland);
EnumConverter.IntegerToEnumValue<E, I> converts integer type to enum value.
IWriterToRef<ulong, Country> converter = EnumConverter.IntegerToEnumValue<Country, ulong>.Instance;
Country country = converter.Read(1UL);
EnumConverter.CreateEnumToEnumConverter is enum-to-enum converter. Conversion is matched by item name.
IEnumDescription countryEnumDescription = EnumDescription.CreateForEnumType(typeof(Country));
IEnumDescription countriesEnumDescription = EnumDescription.CreateForEnumType(typeof(Countries));
IWriterToRef<Country, Countries> converter = (IWriterToRef<Country, Countries>)EnumConverter.CreateEnumToEnumConverter(countryEnumDescription, countriesEnumDescription);
Countries countries = converter.Read(Country.Finland);
.CreateEnumToName() creates converter that converts enum to name.
IWriterToRef<Country, string> converter = (IWriterToRef<Country, string>)EnumDescription.CreateForEnumType(typeof(Country)).CreateEnumToName();
string name = converter.Read(Country.Finland);
.CreateEnumToNames() creates converter that converts enum flags to names.
IWriterToRef<Countries, string[]> converter = (IWriterToRef<Countries, string[]>)EnumDescription.CreateForEnumType(typeof(Countries)).CreateEnumToNames();
string[] names = converter.Read(Countries.Finland | Countries.Sweden);
.CreateNameToEnum() converts name to enum value.
IWriterToRef<string, Country> converter = (IWriterToRef<string, Country>)EnumDescription.CreateForEnumType(typeof(Country)).CreateNameToEnum();
Country country = converter.Read("Finland");
.CreateNamesToEnum() converts names to enum flags.
IWriterToRef<string[], Countries> converter = (IWriterToRef<string[], Countries>)EnumDescription.CreateForEnumType(typeof(Countries)).CreateNamesToEnum();
Countries countries = converter.Read(new string[] { "Finland", "Sweden" });
There are try-creates for various enum converters.
EnumConverter.TryCreateEnumBoxToInteger(typeof(Country), typeof(byte), out EnumConverter.IEnumWriter? writer1);
EnumConverter.TryCreateEnumBoxToInteger(typeof(Countries), typeof(sbyte), out EnumConverter.IEnumWriter? writer2);
EnumConverter.TryCreateEnumValueToInteger(typeof(Country), typeof(short), out EnumConverter.IEnumWriter? writer3);
EnumConverter.TryCreateEnumValueToInteger(typeof(Countries), typeof(ushort), out EnumConverter.IEnumWriter? writer4);
EnumConverter.TryCreateIntegerToEnumBox(typeof(Country), typeof(byte), out EnumConverter.IEnumWriter? writer5);
EnumConverter.TryCreateIntegerToEnumBox(typeof(Countries), typeof(sbyte), out EnumConverter.IEnumWriter? writer6);
EnumConverter.TryCreateIntegerToEnumValue(typeof(Country), typeof(short), out EnumConverter.IEnumWriter? writer7);
EnumConverter.TryCreateIntegerToEnumValue(typeof(Countries), typeof(ushort), out EnumConverter.IEnumWriter? writer8);
.CreateEnumToIndex() creates converter that converts enum to index.
IWriterToRef<Country, int> converter = (IWriterToRef<Country, int>)EnumDescription.Cached[typeof(Country)].CreateEnumToIndex();
int index = converter.Read(Country.Finland);
.CreateEnumToIndices() creates converter that converts enum flags to indices.
IWriterToRef<Countries, int[]> converter = (IWriterToRef<Countries, int[]>)EnumDescription.Cached[typeof(Countries)].CreateEnumToIndices();
int[] indices = converter.Read(Countries.Finland | Countries.Sweden);
.CreateIndexToEnum() converts index to enum value.
IWriterToRef<int, Country> converter = (IWriterToRef<int, Country>)EnumDescription.Cached[typeof(Country)].CreateIndexToEnum();
Country country = converter.Read(0);
.CreateIndicesToEnum() converts indices to enum flags.
IWriterToRef<int[], Countries> converter = (IWriterToRef<int[], Countries>)EnumDescription.Cached[typeof(Countries)].CreateIndicesToEnum();
Countries countries = converter.Read(new int[] { 0, 1 });
[EnumOf] attribute is also conversible with enum types.
// Get [EnumOf]
EnumOfAttribute languageEnumOf = typeof(MyRecord200)
.GetProperty(nameof(MyRecord200.Languages))!
.GetCustomAttribute<EnumOfAttribute>()!;
// Create IEnumDescription
IEnumDescription languageEnumDescription = EnumDescription.CreateForEnumOf(languageEnumOf, null);
IEnumDescription language2EnumDescription = EnumDescription.CreateForEnumType(typeof(Language2));
//
IWriterToRef<string, Language2> converter = (IWriterToRef<string, Language2>)EnumConverter.CreateEnumToEnumConverter(languageEnumDescription, language2EnumDescription);
// Convert
Language2 language = converter.Read("Finnish");
public record MyRecord200
{
[EnumOf<string, Languages>(isFlags: true)]
public string Languages { get; set; } = "Finnish";
}
public class Languages : IEnumerable<IEnumItem<object>>
{
IEnumItem<object>[] content = new IEnumItem<object>[] {
new EnumItem("Languages:Finnish", "Finnish", "Finnish", "Finnish"),
new EnumItem("Languages:Swedish", "Swedish", "Swedish", "Swedish"),
new EnumItem("Languages:Norwegian", "Norwegian", "Norwegian", "Norwegian"),
};
public IEnumerator<IEnumItem<object>> GetEnumerator() => ((IEnumerable<IEnumItem<object>>)content).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => content.GetEnumerator();
}
[Flags]
public enum Language2
{
Finnish = 1 << 0,
Swedish = 1 << 1,
Norwegian = 1 << 2
}
Enum converters can be requested with ConverterRequest.
// Create service stack
IService service = Services.Create(ConverterHandlers.Instance);
// Create enum descriptions
IEnumDescription countryEnumDescription = EnumDescription.CreateForEnumType(typeof(Country));
IEnumDescription countriesEnumDescription = EnumDescription.CreateForEnumType(typeof(Countries));
// Create converter request
ConverterRequest converterRequest = new ConverterRequest(countryEnumDescription, countriesEnumDescription);
// Query converter
IWriterToRef<Country, Countries> converter = service.GetRequired<ConverterRequest, IWriterToRef<Country, Countries>>(converterRequest);
// Convert
Countries countries = converter.Read(Country.Finland);
Full Example
Full example
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Avalanche.Converter;
using Avalanche.Service;
using Avalanche.Utilities;
using Avalanche.Writer;
public class enumconverter
{
public static void Run()
{
{
// <00>
bool ok = EnumConverter.TryGet(typeof(Country), typeof(string), out IWriter converter);
string value = converter.ReadAs<Country, string>(Country.Finland);
// </00>
}
{
// <01>
IWriterToRef<Enum, ulong> converter = EnumConverter.EnumBoxToInteger<Country, ulong>.Instance;
ulong value = converter.Read(Country.Finland);
// </01>
}
{
// <02>
IWriterToRef<ulong, Enum> converter = EnumConverter.IntegerToEnumBox<Country, ulong>.Instance;
Enum @enum = converter.Read(1UL);
// </02>
}
{
// <03>
IWriterToRef<Country, ulong> converter = EnumConverter.EnumValueToInteger<Country, ulong>.Instance;
ulong value = converter.Read(Country.Finland);
// </03>
}
{
// <04>
IWriterToRef<ulong, Country> converter = EnumConverter.IntegerToEnumValue<Country, ulong>.Instance;
Country country = converter.Read(1UL);
// </04>
}
{
// <05>
IEnumDescription countryEnumDescription = EnumDescription.CreateForEnumType(typeof(Country));
IEnumDescription countriesEnumDescription = EnumDescription.CreateForEnumType(typeof(Countries));
IWriterToRef<Country, Countries> converter = (IWriterToRef<Country, Countries>)EnumConverter.CreateEnumToEnumConverter(countryEnumDescription, countriesEnumDescription);
Countries countries = converter.Read(Country.Finland);
// </05>
}
{
// <06>
IWriterToRef<Country, string> converter = (IWriterToRef<Country, string>)EnumDescription.CreateForEnumType(typeof(Country)).CreateEnumToName();
string name = converter.Read(Country.Finland);
// </06>
}
{
// <07>
IWriterToRef<Countries, string[]> converter = (IWriterToRef<Countries, string[]>)EnumDescription.CreateForEnumType(typeof(Countries)).CreateEnumToNames();
string[] names = converter.Read(Countries.Finland | Countries.Sweden);
// </07>
}
{
// <08>
IWriterToRef<string, Country> converter = (IWriterToRef<string, Country>)EnumDescription.CreateForEnumType(typeof(Country)).CreateNameToEnum();
Country country = converter.Read("Finland");
// </08>
}
{
// <09>
IWriterToRef<string[], Countries> converter = (IWriterToRef<string[], Countries>)EnumDescription.CreateForEnumType(typeof(Countries)).CreateNamesToEnum();
Countries countries = converter.Read(new string[] { "Finland", "Sweden" });
// </09>
}
{
// <10>
EnumConverter.TryCreateEnumBoxToInteger(typeof(Country), typeof(byte), out EnumConverter.IEnumWriter? writer1);
EnumConverter.TryCreateEnumBoxToInteger(typeof(Countries), typeof(sbyte), out EnumConverter.IEnumWriter? writer2);
EnumConverter.TryCreateEnumValueToInteger(typeof(Country), typeof(short), out EnumConverter.IEnumWriter? writer3);
EnumConverter.TryCreateEnumValueToInteger(typeof(Countries), typeof(ushort), out EnumConverter.IEnumWriter? writer4);
EnumConverter.TryCreateIntegerToEnumBox(typeof(Country), typeof(byte), out EnumConverter.IEnumWriter? writer5);
EnumConverter.TryCreateIntegerToEnumBox(typeof(Countries), typeof(sbyte), out EnumConverter.IEnumWriter? writer6);
EnumConverter.TryCreateIntegerToEnumValue(typeof(Country), typeof(short), out EnumConverter.IEnumWriter? writer7);
EnumConverter.TryCreateIntegerToEnumValue(typeof(Countries), typeof(ushort), out EnumConverter.IEnumWriter? writer8);
// </10>
}
{
// <16>
IWriterToRef<Country, int> converter = (IWriterToRef<Country, int>)EnumDescription.Cached[typeof(Country)].CreateEnumToIndex();
int index = converter.Read(Country.Finland);
// </16>
}
{
// <17>
IWriterToRef<Countries, int[]> converter = (IWriterToRef<Countries, int[]>)EnumDescription.Cached[typeof(Countries)].CreateEnumToIndices();
int[] indices = converter.Read(Countries.Finland | Countries.Sweden);
// </17>
}
{
// <18>
IWriterToRef<int, Country> converter = (IWriterToRef<int, Country>)EnumDescription.Cached[typeof(Country)].CreateIndexToEnum();
Country country = converter.Read(0);
// </18>
}
{
// <19>
IWriterToRef<int[], Countries> converter = (IWriterToRef<int[], Countries>)EnumDescription.Cached[typeof(Countries)].CreateIndicesToEnum();
Countries countries = converter.Read(new int[] { 0, 1 });
// </19>
}
{
// <20>
// Get [EnumOf]
EnumOfAttribute languageEnumOf = typeof(MyRecord200)
.GetProperty(nameof(MyRecord200.Languages))!
.GetCustomAttribute<EnumOfAttribute>()!;
// Create IEnumDescription
IEnumDescription languageEnumDescription = EnumDescription.CreateForEnumOf(languageEnumOf, null);
IEnumDescription language2EnumDescription = EnumDescription.CreateForEnumType(typeof(Language2));
//
IWriterToRef<string, Language2> converter = (IWriterToRef<string, Language2>)EnumConverter.CreateEnumToEnumConverter(languageEnumDescription, language2EnumDescription);
// Convert
Language2 language = converter.Read("Finnish");
// </20>
}
{
// <30>
// Create service stack
IService service = Services.Create(ConverterHandlers.Instance);
// Create enum descriptions
IEnumDescription countryEnumDescription = EnumDescription.CreateForEnumType(typeof(Country));
IEnumDescription countriesEnumDescription = EnumDescription.CreateForEnumType(typeof(Countries));
// Create converter request
ConverterRequest converterRequest = new ConverterRequest(countryEnumDescription, countriesEnumDescription);
// Query converter
IWriterToRef<Country, Countries> converter = service.GetRequired<ConverterRequest, IWriterToRef<Country, Countries>>(converterRequest);
// Convert
Countries countries = converter.Read(Country.Finland);
// </30>
}
}
}
// <98>
[Flags]
public enum Countries
{
Finland = 1,
Sweden = 2,
Norway = 4
}
public enum Country
{
Finland = 1,
Sweden = 2,
Norway = 3
}
// </98>
// <99>
public record MyRecord200
{
[EnumOf<string, Languages>(isFlags: true)]
public string Languages { get; set; } = "Finnish";
}
public class Languages : IEnumerable<IEnumItem<object>>
{
IEnumItem<object>[] content = new IEnumItem<object>[] {
new EnumItem("Languages:Finnish", "Finnish", "Finnish", "Finnish"),
new EnumItem("Languages:Swedish", "Swedish", "Swedish", "Swedish"),
new EnumItem("Languages:Norwegian", "Norwegian", "Norwegian", "Norwegian"),
};
public IEnumerator<IEnumItem<object>> GetEnumerator() => ((IEnumerable<IEnumItem<object>>)content).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => content.GetEnumerator();
}
[Flags]
public enum Language2
{
Finnish = 1 << 0,
Swedish = 1 << 1,
Norwegian = 1 << 2
}
// </99>