Struggling with generic class... design?
I have
BaseClass
that other classes will extend from:
The extending classes will have properties that BaseClass
doesn't have, do knowing the type is important for deserialization.
Instead of using MyClassA.JsonDeserialiseAs<MyClassA>(json)
every time, I'd like to be able to use MyClassA.JsonDeserialize(json)
What's the best way I can achieve this?2 Replies
My first thought was to make
BaseClass
a generic type, so I can have the following static method:
But this brings in two problems that I'd prefer to avoid:
1. I can't make a generic instance of BaseClass
anymore and will always have to specify the type.
2. I always have to specify the type when checking if something is an instance of BaseClass
.
My next attempt was to make a typed version of BaseClass
which the generic version will extend from, end then other classes from that:
Now I can access MyClassA.JsonDeserialize(json)
, however, MyClassA
doesn't have access to all of the constructors from BaseClass
. Meaning I'd have to duplicate, and rewrite them into BaseClass<T>
, along with any annotations - dumb idea.
I have a feeling the only way I can get what I want is by writing the JsonDeserialize
method into every class that extends BaseClass
In short, my aim is to somehow only have one declaration of the T JsonDeserialize
method, without affecting how BaseClass
is used/accessed and without having to rewrite the same method and annotations 50 timesi'm not sure i understand exactly the issue
is writing each time the whole thing
public static DerivedClass DeserializeMe(string j) => JsonConverter.Deserialize<DerivedClass>(j);
really harder than
public static DerivedClass DeserializeMe(string j) => base.Deserialize<DerivedClass>(j);
because in the end especially for static methods (which can't even be virtual) you have to write something
also you can generate a constructor with quick actions/ctrl+. (or even not using them, if it's just data classes)
you could try if bringing out deserialization from there in a separate architecture so that's not static anymore
but then how would you call all this deserialization, a giant switch?