230V
230V
CC#
Created by 230V on 5/18/2023 in #help
✅ a class implementing a generic interface with more than one interface as one generic type argument
I'm making a serializer (not for JSON), trying hard to use generics and trimmable methods wherever possible. I have:
interface IConverter {} //exists just for the attribute
interface IConverter<T>
{
//the methods could be static, but I'd need to use a bit more reflection than just Activator.CreateInstance<> to call them
void Serialize(TextWriter w, in T t, ...);
void Deserialize(TextReader r, ref T t, ...);
}

///usage: type T { [Converter<X>] V v; } class X : IConverter<V> {}
///built-in converters, such as ParseableFormattableConverter, will be chosen from a dictionary
class ConverterAttribute<T> : Attribute where T : IConverter //can't do T : IConverter<?>
//ex.:
class A : IConverter<Q> {}
class B : IConverter<W> {}
interface IConverter {} //exists just for the attribute
interface IConverter<T>
{
//the methods could be static, but I'd need to use a bit more reflection than just Activator.CreateInstance<> to call them
void Serialize(TextWriter w, in T t, ...);
void Deserialize(TextReader r, ref T t, ...);
}

///usage: type T { [Converter<X>] V v; } class X : IConverter<V> {}
///built-in converters, such as ParseableFormattableConverter, will be chosen from a dictionary
class ConverterAttribute<T> : Attribute where T : IConverter //can't do T : IConverter<?>
//ex.:
class A : IConverter<Q> {}
class B : IConverter<W> {}
Now I want to make a single converter that forwards to IParseable and IFormattable methods, but I can't see how this could be done.
///handles everything that's IParseable and IFormattable, such as DateTime, TimeSpan, long, int, short
///to avoid one converter per each built-in common type (would be an STJ moment)
class ParseableFormattableConverter : IConverter<TCombination/*???*/>
where TCombination : IFormattable, IParseable<?????>
{
void Serialize(TextWriter w, in TCombination t) => t.ToString(null, null);
void Deserialize(TextReader r, ref TCombination t) => t = ref TCombination.Parse(r.Read()); //more or less
}
///handles everything that's IParseable and IFormattable, such as DateTime, TimeSpan, long, int, short
///to avoid one converter per each built-in common type (would be an STJ moment)
class ParseableFormattableConverter : IConverter<TCombination/*???*/>
where TCombination : IFormattable, IParseable<?????>
{
void Serialize(TextWriter w, in TCombination t) => t.ToString(null, null);
void Deserialize(TextReader r, ref TCombination t) => t = ref TCombination.Parse(r.Read()); //more or less
}
33 replies