Base class with abstract subclasses in C #?
public abstract class Request
{
public class Parameters
{
//Threre are no members here
//But there should be in inherited classes
}
public Request()
{
parameters = new Parameters();
}
public Parameters parameters;
}
Two questions:
-
How do I make it so I can add stuff to the constructor but the original constructor is will still be executed?
-
How do I do this so that subclasses can add members to the Parameters class?
source to share
If you're doing what I think you are, you'll have to modify the constructor a bit:
public Request(Parameters parameters) {
this.Parameters = parameters;
}
and then you can do this:
public class SpecificRequest : Request {
public class SpecificRequestParameters : Request.Parameters {
}
public SpecificRequest() : base(new SpecificRequestParameters()) {
//More stuff here
}
}
What specific problem are you trying to solve? What you are doing here seems rather awkward and overly complicated.
source to share
Why are you injecting the Parameters class inside the Request class? Instead, let Request have an instance of parameters and just declare the class somewhere else.
Answer your question 1: Inherit from query will always call the original query constructor.
Question 2: Subclasses cannot add members to parameter classes (except with reflection, but you don't want to go that route). The best you can do is inherit from a parameter and use InheritedRequest in the InheritedParameter class. Note that in this case, you cannot override properties and properties. You will need to have an additional property in the InheritedRequest called AdditionalParameters of type InheritedParameters.
source to share
Constructors can be bound to syntax
ctor() : this()
or
ctor() : base()
there you can pass parameters etc.
Make the Options field shared
abstract class Request<T> where T : Parameters {
T Parameters;
}
class Specialized : Request<SpecialParameters> {
}
where is SpecialParameters
inherited from Parameters
.
source to share