230V
230V
CC#
Created by 230V on 8/15/2024 in #help
✅ deserializing records with STJ results in default values
https://discord.com/channels/143867839282020352/569261465463160900/1273773286311854142 idk why it's null. is there some attribute I don't know about to make it force-assign init?
3 replies
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