© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
32 replies
230V

✅ 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
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Interface implementing generic parameters
C#CC# / help
2y ago
❔ Function with generic argument as a setter
C#CC# / help
3y ago
❔ Class with parameter as a generic parameter
C#CC# / help
4y ago
Using pointers as generic a generic type
C#CC# / help
4y ago