© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
3 replies
UnemployedNinja

Struggling with generic class... design?

I have
BaseClass
BaseClass
that other classes will extend from:
public class BaseClass {
  public BaseClass() { /* Constructor */ }
  public static T? JsonDeserialiseAs<T>(string json) where T : BaseClass { /* Do stuff */ }
}

public class MyClassA : BaseClass {
  public string? SomeProp { get; set; } // Some properties that BaseClass doesn't have
  public MyClassA() : base() { /* Constructor */ }
}
public class BaseClass {
  public BaseClass() { /* Constructor */ }
  public static T? JsonDeserialiseAs<T>(string json) where T : BaseClass { /* Do stuff */ }
}

public class MyClassA : BaseClass {
  public string? SomeProp { get; set; } // Some properties that BaseClass doesn't have
  public MyClassA() : base() { /* Constructor */ }
}
The extending classes will have properties that
BaseClass
BaseClass
doesn't have, do knowing the type is important for deserialization.
Instead of using
MyClassA.JsonDeserialiseAs<MyClassA>(json)
MyClassA.JsonDeserialiseAs<MyClassA>(json)
every time, I'd like to be able to use
MyClassA.JsonDeserialize(json)
MyClassA.JsonDeserialize(json)


What's the best way I can achieve this?
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

❔ generic class in generic method
C#CC# / help
3y ago
Generic Class Help
C#CC# / help
3y ago
Design Pattern for mapping generic class type to implementation
C#CC# / help
3y ago
Generic types API design
C#CC# / help
2y ago